Have to run loop mulitple times to work

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
171
Hey guys, i have a loop in my macros which goes through a range and deletes any cells that contain "(blank)".
the problem is, i have to step through the loop a few times in vba for it to delete all the instances. otherwise some cells with "(blank") still remain.

Code:
Range("date").Select
    For Each cl In Range("date")
    If cl.Formula = "(blank)" Then
    cl.Delete Shift:=xlUp
    End If
Next cl
 
Hey guys, i have a loop in my macros which goes through a range and deletes any cells that contain "(blank)".
the problem is, i have to step through the loop a few times in vba for it to delete all the instances. otherwise some cells with "(blank") still remain.

Code:
Range("date").Select
    For Each cl In Range("date")
    If cl.Formula = "(blank)" Then
    cl.Delete Shift:=xlUp
    End If
Next cl
If the blanks in the "date" range are true blanks (that is, not formulas displaying ""), then you can do it with one line of code...
Code:
[table="width: 500"]
[tr]
	[td]Intersect(ActiveSheet.UsedRange, Range("date").SpecialCells(xlBlanks).EntireRow).Delete[/td]
[/tr]
[/table]
If there is a possibility that the range will not contain any blanks, then you need to protect the code against the error that would be generated for that situation...
Code:
[table="width: 500"]
[tr]
	[td]On Error Resume Next
Intersect(ActiveSheet.UsedRange, Range("date").SpecialCells(xlBlanks).EntireRow).Delete
On Error GoTo 0[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello Rick
According to the OP in post#6
Missed that... thanks.

In that case, still assuming no formula in the cells of the "date" defined name range, it will take two lines of code...
Code:
[table="width: 500"]
[tr]
	[td]Range("date").Replace "(blank)", "", xlWhole, , False, , False, False
Intersect(ActiveSheet.UsedRange, Range("date").SpecialCells(xlBlanks).EntireRow).Delete[/td]
[/tr]
[/table]
And, again, if there might not be any cells marked "(blank)", this...
Code:
[table="width: 500"]
[tr]
	[td]On Error Resume Next
Range("date").Replace "(blank)", "", xlWhole, , False, , False, False
Intersect(ActiveSheet.UsedRange, Range("date").SpecialCells(xlBlanks).EntireRow).Delete
On Error GoTo 0[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,894
Members
449,477
Latest member
panjongshing

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