Deleting blank rows faster than my macro

mayday1

Board Regular
Joined
Oct 5, 2007
Messages
241
The code below deletes rows if column B is empty. It works fine but does about 1 or 2 rows per second and it sometimes has to go through a couple thousand rows. I never know how many rows will be in my file - sometimes 1000, sometimes 4000. I set the limit at 4500 but it's possible there could be more. Is there a faster way?

Dim lRow As Long
Dim iCntr As Long
lRow = 4500
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 2).Value = "" Then
Rows(iCntr).Delete
Application.StatusBar = iCntr
End If
Next
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
The code below deletes rows if column B is empty. It works fine but does about 1 or 2 rows per second and it sometimes has to go through a couple thousand rows. I never know how many rows will be in my file - sometimes 1000, sometimes 4000. I set the limit at 4500 but it's possible there could be more. Is there a faster way?

Dim lRow As Long
Dim iCntr As Long
lRow = 4500
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 2).Value = "" Then
Rows(iCntr).Delete
Application.StatusBar = iCntr
End If
Next


You can try this

Sub DeleteBlankB()
'


Columns("B:B").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
End Sub
 
Upvote 0
You could also simplify that code like
Code:
Columns(2).SpecialCells(xlBlanks).EntireRow.Delete
as it's normally better to avoid selecting things.
 
Upvote 0
You could also simplify that code like
Code:
Columns(2).SpecialCells(xlBlanks).EntireRow.Delete
as it's normally better to avoid selecting things.

I always do everything from recording cause I don't know the language well. :(

This is a nice tip, I always prefer less code if possible. Thanks for adding this
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top