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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

TryingToLearn

Well-known Member
Joined
Sep 10, 2003
Messages
723
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
 

lazyandsenile

New Member
Joined
Mar 27, 2018
Messages
2
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
 

StephenCrump

MrExcel MVP
Joined
Sep 18, 2013
Messages
4,286
Office Version
  1. 365
Platform
  1. Windows
... 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).
 

StephenCrump

MrExcel MVP
Joined
Sep 18, 2013
Messages
4,286
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,143,654
Messages
5,720,098
Members
422,266
Latest member
Mattyw

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
Top