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 21 years, 12 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
-
Uninstalr Updates (Awaiting moderation)
by
jv16
2 hours, 10 minutes ago -
Apple zero days for April
by
Susan Bradley
3 hours, 7 minutes ago -
CVE program gets last-minute funding from CISA – and maybe a new home
by
Nibbled To Death By Ducks
5 hours ago -
Whistleblower describes DOGE IT dept rumpus at America’s labor watchdog
by
Nibbled To Death By Ducks
5 hours, 11 minutes ago -
Seeing BSOD’s on 24H2?
by
Susan Bradley
8 hours ago -
TUT For Private Llama LLM, Local Installation and Isolated from the Internet.
by
bbearren
11 hours, 27 minutes ago -
Upgrade from Windows 10 to 11
by
Holdsworth8
13 hours, 50 minutes ago -
Microsoft : AI-powered deception: Emerging fraud threats and countermeasures
by
Alex5723
16 hours, 40 minutes ago -
0patch
by
WSjcgc50
4 hours, 25 minutes ago -
Devices might encounter blue screen exception with the recent Windows updates
by
Susan Bradley
10 hours, 2 minutes ago -
Windows 11 Insider Preview Build 22631.5261 (23H2) released to Release Preview
by
joep517
19 hours, 40 minutes ago -
Problem opening image attachments
by
RobertG
21 hours, 14 minutes ago -
advice for setting up a new windows computer
by
routtco1001
1 day, 11 hours ago -
It’s Identity Theft Day!
by
Susan Bradley
16 hours, 16 minutes ago -
Android 15 require minimum 32GB of storage
by
Alex5723
1 day, 16 hours ago -
Mac Mini 2018, iPhone 6s 2015 Are Now Vintage
by
Alex5723
1 day, 16 hours ago -
Hertz says hackers stole customer credit card and driver’s license data
by
Alex5723
1 day, 17 hours ago -
Firefox became sluggish
by
Rick Corbett
1 day, 14 hours ago -
Windows 10 Build 19045.5794 (22H2) to Release Preview Channel
by
joep517
1 day, 21 hours ago -
Windows 11 Insider Preview Build 22635.5235 (23H2) released to BETA
by
joep517
1 day, 21 hours ago -
A Funny Thing Happened on the Way to the Forum
by
bbearren
19 hours, 7 minutes ago -
Download speeds only 0.3Mbps after 24H2 upgrade on WiFi and Ethernet
by
John
3 hours, 48 minutes ago -
T-Mobile 5G Wireless Internet
by
WSmmi16
19 hours, 48 minutes ago -
Clock missing above calendar in Windows 10
by
WSCape Sand
21 hours ago -
Formula to Calculate Q1, Q2, Q3, or Q4 of the Year?
by
WSJon5
2 days, 12 hours ago -
The time has come for AI-generated art
by
Catherine Barrett
1 day, 16 hours ago -
Hackers are using two-factor authentication to infect you
by
B. Livingston
2 days, 2 hours ago -
23 and you
by
Max Stul Oppenheimer
2 days, 9 hours ago -
April’s deluge of patches
by
Susan Bradley
13 hours, 33 minutes ago -
Windows 11 Windows Updater question
by
Tex265
3 hours, 28 minutes 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.