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
-
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
2 hours, 51 minutes ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
2 hours, 59 minutes ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
1 hour, 46 minutes ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
12 hours, 49 minutes ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
3 hours, 14 minutes ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
15 hours, 29 minutes ago -
iPhone SE2 Stolen Device Protection
by
Rick Corbett
7 hours, 44 minutes ago -
Some advice for managing my wireless internet gateway
by
LHiggins
3 hours, 25 minutes ago -
NO POWER IN KEYBOARD OR MOUSE
by
HE48AEEXX77WEN4Edbtm
9 hours, 41 minutes ago -
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
1 day ago -
Sometimes I wonder about these bots
by
Susan Bradley
21 hours, 16 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
11 hours, 18 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
1 day, 14 hours ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
1 day, 14 hours ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
2 hours, 33 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
1 day, 17 hours ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
1 day, 17 hours ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
1 day, 6 hours ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
2 days, 1 hour ago -
0Patch, where to begin
by
cassel23
1 day, 19 hours ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
2 days, 15 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
2 days, 3 hours ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
2 days, 23 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
2 days, 14 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
2 days, 2 hours ago -
Installer program can’t read my registry
by
Peobody
5 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
2 days, 12 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
2 days, 20 hours ago -
False error message from eMClient
by
WSSebastian42
3 days, 11 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
3 days, 20 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.