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, 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
-
Button Missing for Automatic Apps Updates
by
pmcjr6142
1 hour, 8 minutes ago -
Ancient SSD thinks it’s new
by
WSila
1 hour, 13 minutes ago -
Washington State lab testing provider exposed health data of 1.6 million people
by
Nibbled To Death By Ducks
5 hours, 13 minutes ago -
WinRE KB5057589 fake out
by
Susan Bradley
1 hour, 49 minutes ago -
The April 2025 Windows RE update might show as unsuccessful in Windows Update
by
Susan Bradley
11 hours, 25 minutes ago -
Firefox 137
by
Charlie
14 hours, 8 minutes ago -
Whisky, a popular Wine frontend for Mac gamers, is no more
by
Alex5723
17 hours, 36 minutes ago -
Windows 11 Insider Preview build 26120.3863 (24H2) released to BETA
by
joep517
17 hours, 49 minutes ago -
Windows 11 Insider Preview build 26200.5551 released to DEV
by
joep517
17 hours, 51 minutes ago -
New Windows 11 PC setup — can I start over in the middle to set up a local id?
by
ctRanger
10 hours, 56 minutes ago -
Windows 11 Insider Preview Build 26100.3902 (24H2) released to Release Preview
by
joep517
21 hours, 23 minutes ago -
Oracle kinda-sorta tells customers it was pwned
by
Nibbled To Death By Ducks
1 day, 3 hours ago -
Global data centers (AI) are driving a big increase in electricity demand
by
Kathy Stevens
1 day, 13 hours ago -
Office apps read-only for family members
by
b
1 day, 16 hours ago -
Defunct domain for Microsoft account
by
CWBillow
1 day, 13 hours ago -
24H2??
by
CWBillow
1 day, 3 hours ago -
W11 23H2 April Updates threw ‘class not registered’
by
WindowsPersister
21 hours, 38 minutes ago -
Master patch listing for April 8th, 2025
by
Susan Bradley
2 hours, 5 minutes ago -
TotalAV safety warning popup
by
Theodore Nicholson
12 hours, 39 minutes ago -
two pages side by side land scape
by
marc
3 days, 14 hours ago -
Deleting obsolete OneNote notebooks
by
afillat
3 days, 16 hours ago -
Word/Outlook 2024 vs Dragon Professional 16
by
Kathy Stevens
2 days, 19 hours ago -
Security Essentials or Defender?
by
MalcolmP
2 days, 21 hours ago -
April 2025 updates out
by
Susan Bradley
16 minutes ago -
Framework to stop selling some PCs in the US due to new tariffs
by
Alex5723
2 days, 15 hours ago -
WARNING about Nvidia driver version 572.83 and 4000/5000 series cards
by
Bob99
2 days, 5 hours ago -
Creating an Index in Word 365
by
CWBillow
3 days, 7 hours ago -
Coming at Word 365 and Table of Contents
by
CWBillow
1 day, 19 hours ago -
Windows 11 Insider Preview Build 22635.5170 (23H2) released to BETA
by
joep517
4 days, 11 hours ago -
Has the Microsoft Account Sharing Problem Been Fixed?
by
jknauth
4 days, 14 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.