Infinite Loop Correction

lazyandsenile

New Member
Joined
Mar 27, 2018
Messages
2
Hello,
I have been trying a lot of different methods to achieve the desired result however close but no cigar.
The code below does what I need but I have an infinite loop which results in a run-time error.
Sub DeleteBlankTesting()​
' This code deletes empty rows if GRNSTATE is empty. Error to end loop​
Sheets("GREEN ATMS").Select​
Do While Not IsEmpty("GRNSTATE")​
Range("GRNSTATE").SpecialCells(xlCellTypeBlanks).Select​
ActiveCell.EntireRow.Delete​
Loop​
End Sub​

The end goal in to find all the blank cells in table range "GRNSTATE" and then delete the entire row.

Any help on this would be legendary. This is the last piece to complete my spreadsheet.

Thank you in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Not sure if your named range is multiple columns but if not, try this:

Code:
Sub DeleteBlankTesting()
' This code deletes empty rows if GRNSTATE is empty. Error to end loop
Sheets("GREEN ATMS").Select
Dim x
Dim c As Range
For x = Right(Range("grnstate").Address, Len(Range("grnstate").Address) - (InStrRev(Range("grnstate").Address, "$"))) _
    To Range("grnstate").Row Step -1
Set c = Cells(x, Range("grnstate").Column)
Debug.Print c.Address
If c = "" Then c.EntireRow.Delete
Next x
End Sub
 
Upvote 0
QUESTIONS ANSWERED.
This is bang on. Thank you so much.

Not sure if your named range is multiple columns but if not, try this:

Code:
Sub DeleteBlankTesting()
' This code deletes empty rows if GRNSTATE is empty. Error to end loop
Sheets("GREEN ATMS").Select
Dim x
Dim c As Range
For x = Right(Range("grnstate").Address, Len(Range("grnstate").Address) - (InStrRev(Range("grnstate").Address, "$"))) _
    To Range("grnstate").Row Step -1
Set c = Cells(x, Range("grnstate").Column)
Debug.Print c.Address
If c = "" Then c.EntireRow.Delete
Next x
End Sub
 
Upvote 0
... or, without looping:

Code:
On Error Resume Next
Range("GRNSTATE").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

I'm also assuming that GRNSTATE has only one column.

If it has multiple columns, presumably you'll want to be deleting rows only where all values in the row are blank (?) which would require slightly different code.

(Note that .SpecialCells(xlCellTypeBlanks) won't include cells where a formula is returning the blank).
 
Upvote 0
Code:
For x = Right(Range("grnstate").Address, Len(Range("grnstate").Address) - (InStrRev(Range("grnstate").Address, "$"))) _
    To Range("grnstate").Row Step -1
    Set c = Cells(x, Range("grnstate").Column)
Next x

And just by the way, you could do this more simply:

Code:
With Range("GRNSTATE")
    For x = .Rows.Count To 1 Step -1
        Set c = .Cells(x, 1)
    Next x
End With
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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