Hello,
I have a vb and need to add commands to format a cell as soon as the person presses enter in column j. For example, range (a1:j1) cells are yellow. I want the row below and the same range format the same as above (a1:j1) as soon as the user enters information in j1. I also want a sequence of numbers in column a. In this case, a1 currently shows a 1. I want a2 to show 2 as soon as the user enters information in columm j and so on. Any help would be great.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
vb formatting cell (200/2003)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » vb formatting cell (200/2003)
- This topic has 13 replies, 5 voices, and was last updated 19 years, 8 months ago.
AuthorTopicWSaaaofpmch
AskWoody LoungerAugust 25, 2005 at 2:19 am #423371Viewing 2 reply threadsAuthorReplies-
WSSammyB
AskWoody LoungerAugust 25, 2005 at 3:11 am #968623The attached workbook should be all that you want and more (see comments in the code for the definition of more). To start out, press the Home key. HTH, Sam
Here is the code for those who do not want to open the workbook:Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range) With Target If .Column 1 Then Exit Sub If .Row Target.Parent.UsedRange.Rows.Count + 1 Then Exit Sub Dim i As Integer ' Increment column A .Value = .Offset(-1, 0) + 1 ' Copy & paste previous row's formats .Offset(-1).EntireRow.Copy .EntireRow.PasteSpecial Paste:=xlPasteFormats For i = 0 To 9 ' If previous row has a formula, copy it If .Offset(-1, i).HasFormula Then _ .Offset(-1, i).Copy .Offset(0, i) Next i .Next.Select ' Skip to column B End With End Sub
-
H. Legare Coleman
AskWoody PlusAugust 25, 2005 at 4:52 pm #968744I think that Sammy’s code has a number of problems. It uses the Selection Change event which will introduce a lot of extra unnessary overhead. It will not do what you want if you paste values into a number of cells in column J at the same time. It will put the incorrect value in column A if the cell in column A in the row above the cell in column J that was changed does not have a value in it. It also copies formulas from the other cells in the range A1:J1 in the row above the row changed to the row that was changed, and you did not ask for that to happen. I would recommend replacing Sammy’s code with this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range, oARng As Range
If Intersect(Target, Range("J:J")) Is Nothing Then Exit Sub
For Each oCell In Intersect(Target, Range("J:J"))
If oCell.Value "" Then
Set oARng = Selection
If oCell.Row = 1 Then
oCell.Offset(0, -9).Value = 1
Else
Application.EnableEvents = False
Range("A1:J1").Offset(oCell.Row - 2, 0).Copy
Range("A1:J1").Offset(oCell.Row - 1, 0).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
If IsNumeric(Range("A1").Offset(oCell.Row - 2, 0)) And Range("A1").Offset(oCell.Row - 2, 0) "" Then
Range("A1").Offset(oCell.Row - 1, 0) = Range("A1").Offset(oCell.Row - 2, 0) + 1
End If
oARng.Select
Application.EnableEvents = True
End If
End If
Next oCell
End Sub
-
WSSammyB
AskWoody LoungerAugust 25, 2005 at 4:28 pm #968775> Sammy’s code has a number of problems
Can I plead it was Midnight when I posted?On the way to work this morning, I was thinking of adding an On Error Bail statement. It will not be pretty if the Target is A1!
> It uses the Selection Change event
I like your idea of using the Worksheet Change event, but if column J is left empty, then it will not fire, so there may be a problem there. But, why is the Selection Change more overhead?> It will not do what you want if you paste values into a number of cells in column J.
True, I never thought of that> It also copies formulas from the other cells in the range A1:J1 in the row above
I confess, I changed the design specs.But, I bet that aaa will like it so much that she will want it!
Two questions:
Your line
-
H. Legare Coleman
AskWoody PlusAugust 25, 2005 at 5:06 pm #968792I haven’t tested the code, but I also don’t think your code will do anything if you change column J and hit tab to go to column K.
The selection change will be more overhead, because the event will fire every time the selection changes. For example, if I tab from cell to cell. Since nothing on the sheet changed, you are executing the event routine for no reason.
Intersect(Target, Range(“J:J”))
-
-
-
WSSammyB
AskWoody LoungerAugust 25, 2005 at 7:09 pm #968874OK, so I think that we have several questions:
1) Will the user ever not make an entry in column J? If so, do you still want the auto-formatting when he goes to column A of the next row?
2) Do you want my copy formula feature?
3) Do you want to be able to paste more than one row of data and then have the auto-formatting occur on each of the lines or will there always be just a single line entry?With these answers, someone will rewrite the code for you. HTH –Sam
-
H. Legare Coleman
AskWoody PlusAugust 25, 2005 at 7:22 pm #968878Sammy: Just one point of clarification. The original poster never said “when he goes to column A of the next row.” What he said was “as soon as the person presses enter in column j.” Pressing Enter in column J could take you to a number of different places, depending on how the “Move selection after enter” option is set in the Edit tab of Tools/Options, and on what is selected when enter is hit. That is another reason I used the worksheet change event.
-
WSSammyB
AskWoody LoungerAugust 25, 2005 at 7:59 pm #968884Yes, exactly. I assumed (always dangerous) that the user would always be enterting data & pressing the tab key to proceed to the cell to the right, then finally pressing the enter key after making an entry in column J to return to column A of the next row. Doing these automatic things is always dangerous, especially if you have multiple users. I have a spreadsheet that does running totals and inserts new lines in the middle of the sheet at appropriate times. I must have redesigned it a dozen times before I got predictible, useable, and appropriate behavior.
-
WSaaaofpmch
AskWoody LoungerAugust 25, 2005 at 9:43 pm #968918Sorry for all the confusion guys, but I still don’t know which code to use. Basically, I am attaching the file for reference. I want the person to fill all cells until they reach D7(which is merge all the way to J7). If any text is enter in this cell (d7) I want the worksheet to format the row below and put a 2(subsequent number from above)in A2 and format the same as the row above. I hope this helps to clarify the issue. Thanks.
-
H. Legare Coleman
AskWoody Plus -
WSaaaofpmch
AskWoody LoungerAugust 26, 2005 at 3:39 am #968958 -
WSsdckapr
AskWoody LoungerAugust 26, 2005 at 9:08 am #968982If you copy the worksheet with this code into a different book, the code will go with it, since the code is part of the worksheet.
Code like:
Workbooks("FormatRows.xls").Worksheets("Notes").Copy _ Before:=Workbooks("OtherWorkbook.xls").Sheets(1)
can be used to copy it. Change the workbook and sheet names as desired.
Steve
-
WSaaaofpmch
AskWoody LoungerSeptember 3, 2005 at 10:30 pm #970762I don’t think that is going to work. I am not trying to copy the code to another sheet, I am trying to included in another VB macro that adds the notes spreadsheet. I might be misunderstanding, but the code that was given to me applies only if the sheet exist. What do I do if I have a VB macro to add the sheet and I want to make the code part of it. Any help would be great.
-
WSHansV
AskWoody LoungerSeptember 3, 2005 at 11:01 pm #970765By far the easiest way is to include the sheet complete with the code in the workbook, hidden if you like, and to copy it when you need a new one (and unhide it if necessary).
It is possible to add the code using a macro, but that requires that the user has ticked “Trust access to Visual Basic project” in the Trusted Sources tab of Tools | Macro | Security.
You must set a reference (in Tools | References… in the Visual Basic Editor) to the Microsoft Visual Basic for Applications Extensibility 5.3 library.
The code to create a new worksheet, and to add the Worksheet_Change event procedure is:
Sub AddSheetWithCode()
Dim wsh As Worksheet
Dim mdl As CodeModule
Dim lngLine As Long
Set wsh = Worksheets.Add
Set mdl = ActiveWorkbook.VBProject.VBComponents(wsh.Name).CodeModule
lngLine = mdl.CreateEventProc("Change", "Worksheet")
mdl.InsertLines lngLine + 1, _
" Dim oCell As Range, oARng As Range" & vbCrLf & _
" If Intersect(Target, Range(""D7:D65536"")) Is Nothing Then Exit Sub" & vbCrLf & _
" For Each oCell In Intersect(Target, Range(""D7:D65536""))" & vbCrLf & _
" If oCell.Value """" Then" & vbCrLf & _
" Set oARng = Selection" & vbCrLf & _
" Application.EnableEvents = False" & vbCrLf & _
" Range(""A1:J1"").Offset(oCell.Row - 1, 0).Copy" & vbCrLf & _
" Range(""A1:J1"").Offset(oCell.Row, 0).PasteSpecial Paste:=xlPasteFormats" & _
vbCrLf & _
" Application.CutCopyMode = False" & vbCrLf & _
" If IsNumeric(Range(""A1"").Offset(oCell.Row - 1, 0)) Then" & vbCrLf & _
" Range(""A1"").Offset(oCell.Row, 0) = " & _
"Range(""A1"").Offset(oCell.Row - 1, 0) + 1" & vbCrLf & _
" End If" & vbCrLf & _
" oARng.Select" & vbCrLf & _
" Application.EnableEvents = True" & vbCrLf & _
" End If" & vbCrLf & _
" Next oCell"
End Sub
-
-
-
-
Viewing 2 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
8 minutes ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
30 minutes ago -
Outdated Laptop
by
jdamkeene
5 hours, 33 minutes ago -
Updating Keepass2Android
by
CBFPD-Chief115
10 hours, 58 minutes ago -
Another big Microsoft layoff
by
Charlie
10 hours, 38 minutes ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
1 hour, 36 minutes ago -
May 2025 updates are out
by
Susan Bradley
11 hours, 3 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
16 hours, 42 minutes ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
16 hours, 44 minutes ago -
Drivers suggested via Windows Update
by
Tex265
16 hours, 35 minutes ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
14 hours, 19 minutes ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
23 hours, 34 minutes ago -
Apple releases 18.5
by
Susan Bradley
18 hours ago -
Fedora Linux 40 will go end of life for updates and support on 2025-05-13.
by
Alex5723
1 day, 1 hour ago -
How a new type of AI is helping police skirt facial recognition bans
by
Alex5723
1 day, 1 hour ago -
Windows 7 ISO /Windows 10 ISO
by
ECWS
8 hours, 54 minutes ago -
No HP software folders
by
fpefpe
1 day, 9 hours ago -
Which antivirus apps and VPNs are the most secure in 2025?
by
B. Livingston
6 hours, 39 minutes ago -
Stay connected anywhere
by
Peter Deegan
1 day, 14 hours ago -
Copilot, under the table
by
Will Fastie
1 day, 5 hours ago -
The Windows experience
by
Will Fastie
1 day, 20 hours ago -
A tale of two operating systems
by
Susan Bradley
1 hour, 9 minutes ago -
Microsoft : Resolving Blue Screen errors in Windows
by
Alex5723
2 days, 2 hours ago -
Where’s the cache today?
by
Up2you2
2 days, 17 hours ago -
Ascension says recent data breach affects over 430,000 patients
by
Nibbled To Death By Ducks
2 days, 10 hours ago -
Nintendo Switch 2 has a remote killing switch
by
Alex5723
1 day, 10 hours ago -
Blocking Search (on task bar) from going to web
by
HenryW
12 hours, 56 minutes ago -
Windows 10: Microsoft 365 Apps will be supported up to Oct. 10 2028
by
Alex5723
3 days, 10 hours ago -
Add or Remove “Ask Copilot” Context Menu in Windows 11 and 10
by
Alex5723
3 days, 10 hours ago -
regarding april update and may update
by
heybengbeng
3 days, 12 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.