I hope someone will be able to help, I have a report in range A1:J1000 and is sorted by a part number that appears in row D. I need a macro that would insert a row each time the value in D changes.
Thank you for any help you may be able to give,
Stats
![]() |
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 |
-
Insert Rows on Condition (excel 2000)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Insert Rows on Condition (excel 2000)
- This topic has 12 replies, 6 voices, and was last updated 22 years, 9 months ago.
AuthorTopicWSStats
AskWoody LoungerJuly 29, 2002 at 4:58 pm #374241Viewing 0 reply threadsAuthorReplies-
WSJohnBF
AskWoody LoungerJuly 30, 2002 at 1:59 pm #604821Just checking; do you want to insert this row in order to insert a subtotal or count or such? If so, Data, Subtotals has capabilities like this.
[Edit]
Here’s code to do it if required:Sub NewRowOnChange()
Dim rngCell As Range
For Each rngCell In _
ThisWorkbook.Worksheets(“Sheet1”).Columns(“D:D”).SpecialCells(xlCellTypeConstants, 3)
If rngCell.Row > 1 Then
If rngCell.Offset(-1, 0).Value “” _
And rngCell.Value rngCell.Offset(-1, 0).Value Then _
rngCell.EntireRow.Insert Shift:=xlDown
End If
Next rngCell
End Sub -
WSJailAdmin
AskWoody Lounger -
WSJohnBF
AskWoody Lounger -
WSStats
AskWoody LoungerJuly 30, 2002 at 9:19 am #604915Thanks for your reply John. No, I am not deleting the rows after putting them in. It’s a report that pulls some information from our SQL Database using MS Query and outputs a report of what products have to be produced in the near future. Each part can appear on the report one time or it can appear a dozen times depnding on how many orders are outstanding but it is sorted by the part number in Col. D. Inserting the blank row between each part makes it easier to look at.
When I ran your code I got an “Application-defined or object-defined error”. The sheet in the workbook is called “Sheet1” and I couldn’t find any other “variables” I would have to change in the code. Have I missed something. -
WSsdckapr
AskWoody Lounger -
WSHansV
AskWoody LoungerJuly 30, 2002 at 10:02 am #604920John’s code runs into problems in cell D1, because it tries to refer to the cell above, which doesn’t exist. And I think it’s better to step through the range backwards.
Try this:
Sub NewRowOnChange()
Dim rng As Range
Dim lngRow As Long
Set rng = Range(“D1”, Range(“D65536”).End(xlUp))
For lngRow = rng.Rows.Count To 2 Step -1
If rng(lngRow – 1) “” And rng(lngRow) “” And _
rng(lngRow) rng(lngRow – 1) Then
rng(lngRow).EntireRow.Insert
End If
Next lngRow
End SubThis code steps backwards and stops at row 2. If you want to prevent the code from inserting a row above row 2 (for instance because the first row contains field names), replace 2 by 3 (or whatever you want) in the instruction For lngRow = rng.Rows.Count To 2 Step -1
-
WSJohnBF
AskWoody Lounger -
WSStats
AskWoody Lounger -
WSStats
AskWoody LoungerJuly 31, 2002 at 1:17 pm #605226 -
WSHansV
AskWoody LoungerJuly 31, 2002 at 1:27 pm #605241Hello Stats,
This macro will remove rows whose cell in column D is empty:
Sub DeleteEmptyRows()
Dim rng As Range
Dim lngRow As Long
Set rng = Range(“D1”, Range(“D65536”).End(xlUp))
For lngRow = rng.Rows.Count To 2 Step -1
If rng(lngRow) = “” Then
rng(lngRow).EntireRow.Delete
End If
Next lngRow
End SubYou can execute this macro manually before closing the workbook, or call it from the Workbook_BeforeClose event, so that it will be run automatically.
-
WSColinBurrows
AskWoody LoungerJuly 31, 2002 at 4:51 pm #605308FWIW, an alternative approach that doesn’t (necessarily) use macros… Create a separate list of unique part numbers (Data | Filter | Advanced Filter); append them at the bottom of your list; make them “disappear” – either with a custom number format (;;
, or by making the font color the same as the background; then sort the newly-extended list with part number as the primary key and some other (non-blank) field as the secondary. Each new part number would then be preceded by an (apparently) blank row. Removing the rows would mean sorting by the secondary key to group together the inserted rows, deleting them, and then sorting back into part number sequence.
The above could be made into a macro, although it would probably be more complicated than the solutions already provided which I’m sure work fine. The only potential reason for considering the above would be if you have a very large list: the above technique would run much quicker than the row insertion macros. -
WSJohnBF
AskWoody Lounger
-
-
-
-
Viewing 0 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
-
Which antivirus apps and VPNs are the most secure in 2025?
by
B. Livingston
1 hour, 21 minutes ago -
Stay connected anywhere
by
Peter Deegan
1 hour, 23 minutes ago -
Copilot, under the table
by
Will Fastie
30 seconds ago -
The Windows experience
by
Will Fastie
13 minutes ago -
A tale of two operating systems
by
Susan Bradley
8 minutes ago -
Microsoft : Resolving Blue Screen errors in Windows
by
Alex5723
3 hours, 29 minutes ago -
Where’s the cache today?
by
Up2you2
18 hours, 49 minutes ago -
Ascension says recent data breach affects over 430,000 patients
by
Nibbled To Death By Ducks
11 hours, 35 minutes ago -
Nintendo Switch 2 has a remote killing switch
by
Alex5723
2 hours, 34 minutes ago -
Blocking Search (on task bar) from going to web
by
HenryW
19 hours, 31 minutes ago -
Windows 10: Microsoft 365 Apps will be supported up to Oct. 10 2028
by
Alex5723
1 day, 12 hours ago -
Add or Remove “Ask Copilot” Context Menu in Windows 11 and 10
by
Alex5723
1 day, 12 hours ago -
regarding april update and may update
by
heybengbeng
1 day, 13 hours ago -
MS Passkey
by
pmruzicka
15 hours, 36 minutes ago -
Can’t make Opera my default browser
by
bmeacham
1 day, 21 hours ago -
*Some settings are managed by your organization
by
rlowe44
1 day, 8 hours ago -
Formatting of “Forward”ed e-mails
by
Scott Mills
1 day, 20 hours ago -
SmartSwitch PC Updates will only be supported through the MS Store Going Forward
by
PL1
2 days, 15 hours ago -
CISA warns of hackers targeting critical oil infrastructure
by
Nibbled To Death By Ducks
3 days ago -
AI slop
by
Susan Bradley
18 hours, 51 minutes ago -
Chrome : Using AI with Enhanced Protection mode
by
Alex5723
3 days, 2 hours ago -
Two blank icons
by
CR2
10 hours, 24 minutes ago -
Documents, Pictures, Desktop on OneDrive in Windows 11
by
ThePhoenix
12 hours, 21 minutes ago -
End of 10
by
Alex5723
3 days, 13 hours ago -
Single account cannot access printer’s automatic duplex functionality
by
Bruce
2 days, 11 hours ago -
test post
by
gtd12345
3 days, 19 hours ago -
Privacy and the Real ID
by
Susan Bradley
3 days, 9 hours ago -
MS-DEFCON 2: Deferring that upgrade
by
Susan Bradley
1 day, 12 hours ago -
Cant log on to oldergeeks.Com
by
WSJonharnew
4 days ago -
Upgrading from Win 10
by
WSjcgc50
2 days, 11 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.