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
-
Test post
by
Susan Bradley
3 hours, 53 minutes ago -
Used Systems to delete Temp files Gone WRONG what does this mean?
by
Deo
2 hours, 58 minutes ago -
SSD shuts down on its own
by
CWBillow
5 hours, 19 minutes ago -
OneDrive File Sharing Changes
by
David Clark
7 hours, 45 minutes ago -
OneDrive File Sharing Changes
by
David Clark
9 hours, 49 minutes ago -
Win 10 Pro 22H2 to Win 11 Pro 23H2 Conversion Guide
by
doneager
4 hours, 36 minutes ago -
Today is world backup day
by
Alex5723
1 hour, 26 minutes ago -
Windows .exe on Mint
by
Slowpoke47
11 hours, 22 minutes ago -
Reviewing your licensing options
by
Susan Bradley
2 hours, 53 minutes ago -
Apple has been analyzing your photos since September 2024
by
B. Livingston
6 hours, 26 minutes ago -
What Windows 11 24H2 offers beyond bugs
by
Lance Whitney
1 hour, 43 minutes ago -
Making sense of Settings in Windows 11
by
Simon Bisson
3 hours, 48 minutes ago -
Windows 11 pro fails to log in after upgrading Win 10 pro to Win 11 pro 24h2
by
ben_sitaud
7 hours, 39 minutes ago -
23H2 / 24H2 / Local v. Microsoft Account.
by
CWBillow
5 hours, 25 minutes ago -
YouTube Ad Blocker Blocker
by
bbearren
5 hours, 36 minutes ago -
Obscure historical facts about Windows
by
Cybertooth
7 hours, 14 minutes ago -
Microsoft Backup
by
Linda2019
1 hour, 14 minutes ago -
What is the best notepad++ version for W7?
by
Picky
6 hours, 28 minutes ago -
What are right steps to move MS 365 Office+OneDrive files from PC to iMac?
by
glnz
1 day, 16 hours ago -
How to move existing MS 365 Office with OneDrive files from PC to new iMac
by
glnz
1 day, 16 hours ago -
How to move MS 365 files (some on OneDrive) from PC to iMac
by
glnz
2 days, 11 hours ago -
Microsoft adding Quick Machine Recovery to Windows 11
by
Alex5723
2 days, 11 hours ago -
Microsoft vs Passwords
by
Alex5723
1 day, 19 hours ago -
Windows 11 Insider Preview build 26200.5516 released to DEV
by
joep517
2 days, 15 hours ago -
Windows 11 Insider Preview build 26120.3653 (24H2) released to BETA
by
joep517
2 days, 15 hours ago -
Two March KB5053606 updates?
by
Adam
2 days, 8 hours ago -
MS Edge Not Updating to v134.0.3124.95 (rel. 27-Mar-2025)
by
lmacri
2 days, 9 hours ago -
Intel® Graphics/Sound Driver updates for 7th-10th Gen Intel® Core™ Processor
by
Alex5723
2 days, 11 hours ago -
Is there a comprehensve way to tranfer ALL current Edge Settings into a new Edge
by
Tex265
2 days, 10 hours ago -
Transferring ALL info/settings from current Firefox to new computer Firefox
by
Tex265
2 days, 9 hours 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.