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
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
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 21 years, 11 months 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
-
Photos from iPhone to Win 10 duplicating/reformatting to .mov
by
J9438
2 hours, 52 minutes ago -
Thunderbird in trouble. Here comes Thundermail
by
Alex5723
5 hours, 1 minute ago -
Get back ” Open With” in context menus
by
CWBillow
5 hours, 26 minutes ago -
Many AMD Ryzen 9800X3D on ASRock have died
by
Alex5723
9 hours, 13 minutes ago -
simple general stupid question
by
WSaltamirano
3 hours, 13 minutes ago -
Automatic Toggle code for pages and bookmarks for Table of Content (Awaiting moderation)
by
switchtolinux
13 hours, 35 minutes ago -
April 2025 Office non-Security updates
by
PKCano
5 hours, 20 minutes ago -
Microsoft wants to hear from you
by
Will Fastie
21 hours, 27 minutes ago -
Windows 11 Insider Preview Build 22635.5160 (23H2) released to BETA
by
joep517
23 hours, 48 minutes ago -
Europe Seeks Alternatives to U.S. Cloud Providers
by
Alex5723
1 day, 5 hours ago -
Test post
by
Susan Bradley
1 day, 7 hours ago -
Used Systems to delete Temp files Gone WRONG what does this mean?
by
Deo
1 day, 9 hours ago -
SSD shuts down on its own
by
CWBillow
1 day ago -
OneDrive File Sharing Changes
by
David Clark
1 day, 17 hours ago -
OneDrive File Sharing Changes
by
David Clark
1 day, 19 hours ago -
Win 10 Pro 22H2 to Win 11 Pro 23H2 Conversion Guide
by
doneager
19 hours, 25 minutes ago -
Today is world backup day
by
Alex5723
1 day, 10 hours ago -
Windows .exe on Mint
by
Slowpoke47
1 day, 20 hours ago -
Reviewing your licensing options
by
Susan Bradley
1 day, 6 hours ago -
Apple has been analyzing your photos since September 2024
by
B. Livingston
16 hours, 11 minutes ago -
What Windows 11 24H2 offers beyond bugs
by
Lance Whitney
12 hours, 2 minutes ago -
Making sense of Settings in Windows 11
by
Simon Bisson
13 hours, 22 minutes ago -
Windows 11 pro fails to log in after upgrading Win 10 pro to Win 11 pro 24h2
by
ben_sitaud
1 day, 17 hours ago -
23H2 / 24H2 / Local v. Microsoft Account.
by
CWBillow
1 day, 14 hours ago -
YouTube Ad Blocker Blocker
by
bbearren
1 day, 15 hours ago -
Obscure historical facts about Windows
by
Cybertooth
1 day, 16 hours ago -
Microsoft Backup
by
Linda2019
1 day, 8 hours ago -
What is the best notepad++ version for W7?
by
Picky
1 day, 16 hours ago -
What are right steps to move MS 365 Office+OneDrive files from PC to iMac?
by
glnz
3 days, 1 hour ago -
How to move existing MS 365 Office with OneDrive files from PC to new iMac
by
glnz
3 days, 1 hour ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
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.