I think the following macro that i’ve written takes too long and is inefficient but i don’t know how to correct it. Could someone help me with it. I want to delete all rows that are empty OR have a spaceband ” ” in them. I use this macro to delete rows from csv’s that i get and they “look” blank but have a space in one of the cells. These rows still need to be deleted. Thank you
![]() |
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 |
-
Inefficient macro (Excel xp)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Inefficient macro (Excel xp)
- This topic has 19 replies, 7 voices, and was last updated 22 years, 1 month ago.
Viewing 1 reply threadAuthorReplies-
WSHansV
AskWoody Lounger -
WSjha900
AskWoody LoungerApril 20, 2003 at 1:47 am #669857oops sorry.
Sub DeleteRowsSimple()
Dim I As LongOn Error Resume Next
With ActiveSheet
lLastRow = .Range(“A65536”).End(xlUp).Row – 1
For I = lLastRow To 0 Step -1
If .Range(“B” & I).Value = “” And .Range(“C” & I).Value = “” And .Range(“D” & I).Value = “” And .Range(“E” & I).Value = “” And .Range(“F” & I).Value = “” And .Range(“G” & I).Value = “” And .Range(“H” & I).Value = “” And _
.Range(“A” & I).Value = “” Or .Range(“A” & I).Value = ” ” Then
.Range(“A” & I).Offset(0, 0).EntireRow.Delete
End If
Next I
End With
Range(“A1”).Select
End Sub -
H. Legare Coleman
AskWoody PlusApril 20, 2003 at 2:37 am #669862Your macro does not seem to do exactly what you described in your first post. I think that the code below does what you described:
Sub DeleteRowsSimple() Dim I As Long, J As Long Dim lLastRow As Long Dim strWk As String With ActiveSheet lLastRow = .UsedRange.Row + .UsedRange.Rows.Count - 2 For I = lLastRow To 0 Step -1 strWk = "" For J = 0 To 7 strWk = strWk & .Range("A1").Offset(I, J).Value Next J If Trim(strWk) = "" Then .Range("A1").Offset(I, 0).EntireRow.Delete End If Next I End With Range("A1").Select End Sub
-
WSAlanMiller
AskWoody LoungerApril 20, 2003 at 12:49 pm #669903I wouldn’t dare to try to improve on the solution offered by Legare
, but I’d just add that I’ve been caught with such “inefficient” code myself. The key to improvement lies in identifying the used range and processing only within that, rather than the entire (very large, and largely empty) sheet. This is exactly what Legare has done in his solution.
Alan
-
H. Legare Coleman
AskWoody PlusApril 20, 2003 at 12:58 pm #669906Good point, but not exactly in this case. What you say is frequently true. However, in this case, I used the UsedRange property to identify the last row used, even if it did not have anything in column A in that row. I then process from there up to the top row because it looked like jha900 wanted to delete unused rows at the top of the sheet also. The way he was finding the last row would not work if there were rows at the bottom of the sheet with empty cells in column A.
-
WSAlanMiller
AskWoody LoungerApril 21, 2003 at 2:20 am #669972Thanks for the qualification Legare. I must admit to not looking deeply enough to spot that issue. The one that always dragged down the performance of some of my early macro attempts, was iterating the whole sheet rather than just the used range. But this pops up a new question:
Does the used range always begin at A1?
If I created a new sheet and started entering data into D5, and maintained cell D5 as the top left cell for data entry, what would the used range look like?
Alan
-
H. Legare Coleman
AskWoody PlusApril 21, 2003 at 4:41 am #669978No, the UsedRange does not always start at A1. The UsedRange starts at the cell at the intersection of the first row and first column that contain data. So, if the first row that contains data is row 5, and the first column that contains data is D, and the last row that contains data is row 50, and the last column that contains data is column G, then the UsedRange is D5:G50. That is why I added UsedRange.Row (five in the previous example) and UsedRange.Rows.Count (46 in the previous example) to find the last row.
-
WSAlanMiller
AskWoody LoungerApril 21, 2003 at 2:47 pm #670010(Edited by AlanMiller on 22-Apr-03 02:47. )
Excellent! Thanks for clearing that one up. I was pretty sure where the used range finished; always a bit unclear on where it started though. Now I can sleep easy
.
And just to qualify this further, would it also be correct to describe it as the “minimal” rectangle that contains all of the non-empty cells?
Alan
-
H. Legare Coleman
AskWoody Plus -
WSAlanMiller
AskWoody LoungerApril 21, 2003 at 10:57 pm #670134Thank you again Legare. You’ve also preempted my question about “deleted” cells being included in the UsedRange, which is something I’ve observed. However, I seem to recall an issue with the UsedRange sometimes spreading into empty regions, even after the sheet is saved and reopened. I’ve certainly noticed this when people have “fiddled” with a worksheet, but have not permanently added any extra cells by the time they’d resaved it and I get it back. Is this not one of the “bloat” issues that can be addressed, by copying to a new sheet and resaving the workbook? Maybe old “ghost” formatting remains in those “empty” cells?
Alan
-
WSHansV
AskWoody Lounger -
WSErrolv
AskWoody LoungerApril 22, 2003 at 12:22 am #670183FWIW:
I have a spreadsheet where I need to delete many unused rows, too. Once I establish the total range of rows, I insert a new column A, fill it with consecutive numbers from the first to the last used rows. This is for indexing. Then I SORT on a column just to get the blank “” and space ” ” rows together. FIND them, DELETE them, and, finally SORT on the Column A index. Delete the temporary Column A, and the empty rows will be gone.This method uses more of Excel’s methods to do the looking.
-
WSHansV
AskWoody LoungerApril 22, 2003 at 12:47 am #670205 -
WSAlanMiller
AskWoody Lounger -
WSErrolv
AskWoody LoungerApril 23, 2003 at 12:30 pm #670681Alan,
I described the “index column” sorting method in a generic way just to pass on the idea. You can do any kind of sort to group the rows, then have your way with the data. The sheet I work on does have blank rows when I’m done because of the way I sort and the design of the rows. Also, the rows have many links to other sheets, and all that sorting and deleting has no affect on the links.Errol
-
WSAlanMiller
AskWoody Lounger
-
-
WSjha900
AskWoody Lounger -
WSJohnBF
AskWoody LoungerApril 22, 2003 at 2:52 pm #670371As a side note, Jan Karel pieterse in post 180759 demonstrated a very slick Autofilter way to delete blanks in a particular column. It will not fit specific or complex requirements such as “if the row A cell is blank and the row D cell contains ‘**’ and today is Tuesday”, but it can use any criteria available in AutoFilter, including filtering on blanks and spaces. The generic version of Jan Karel’s code which I use regularly on a certain report I receive is:
Sub DelBlanks()
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:=”=” ‘ filter for blanks, including spaces
‘ next line assumes that first UsedRange.Row is Filter data or Filter Headers
Rows(ActiveSheet.UsedRange.Row + 1 & “:” & ActiveSheet.UsedRange.Row _
+ ActiveSheet.UsedRange.Rows.Count).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
End SubIf you can use it, it sure beats coding a loop.
-
-
-
-
-
WSVBNerd
AskWoody LoungerApril 24, 2003 at 5:06 pm #671033A little trick that also speeds up macros is
At the beginning of your macro..
Application.Screenupdating = FalseThen at the end of your macro…
Application.Screenupdating = TrueOf course, if you have any interaction with the user, this won’t work. You’d need to hop in and out of the screen updating.
Viewing 1 reply thread -

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
-
Windows 11 Insider Preview build 26200.5603 released to DEV
by
joep517
1 hour, 49 minutes ago -
Windows 11 Insider Preview build 26120.4151 (24H2) released to BETA
by
joep517
1 hour, 50 minutes ago -
Fixing Windows 24H2 failed KB5058411 install
by
Alex5723
5 hours ago -
Out of band for Windows 10
by
Susan Bradley
6 hours, 34 minutes ago -
Giving UniGetUi a test run.
by
RetiredGeek
13 hours, 31 minutes ago -
Windows 11 Insider Preview Build 26100.4188 (24H2) released to Release Preview
by
joep517
21 hours, 8 minutes ago -
Microsoft is now putting quantum encryption in Windows builds
by
Alex5723
19 hours, 6 minutes ago -
Auto Time Zone Adjustment
by
wadeer
1 day, 1 hour ago -
To download Win 11 Pro 23H2 ISO.
by
Eddieloh
23 hours, 17 minutes ago -
Manage your browsing experience with Edge
by
Mary Branscombe
3 hours, 53 minutes ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
14 hours, 49 minutes ago -
Hobbies — There’s free software for that!
by
Deanna McElveen
19 hours, 6 minutes ago -
Apps included with macOS
by
Will Fastie
18 hours, 44 minutes ago -
Xfinity home internet
by
MrJimPhelps
18 hours, 39 minutes ago -
Convert PowerPoint presentation to Impress
by
RetiredGeek
18 hours, 37 minutes ago -
Debian 12.11 released
by
Alex5723
1 day, 22 hours ago -
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
2 days, 2 hours ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
1 day, 5 hours ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
22 hours, 18 minutes ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
2 days, 19 hours ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
2 days, 9 hours ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
7 hours, 29 minutes ago -
iPhone SE2 Stolen Device Protection
by
Rick Corbett
2 days, 14 hours ago -
Some advice for managing my wireless internet gateway
by
LHiggins
1 day, 21 hours ago -
NO POWER IN KEYBOARD OR MOUSE
by
HE48AEEXX77WEN4Edbtm
23 hours, 48 minutes ago -
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
3 days, 7 hours ago -
Sometimes I wonder about these bots
by
Susan Bradley
19 hours, 38 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
2 days, 17 hours ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
3 days, 21 hours ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
2 days, 5 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.