Looping Through all Blank Cells

dhancy

Board Regular
Joined
Jul 10, 2013
Messages
125
Office Version
  1. 365
Platform
  1. Windows
Hello,

My goal is to find all blank cells in a column, and when I find those cells, I want to delete the entire row.

My data has two blank cells in column B.

In the code below, the first debug statement correctly recognizes the two blank cells.

However, the For loop stops after only one iteration.

Any ideas what I am missing?


Here is my code:

VBA Code:
Dim lastRow As Integer
Dim rng As Range
Dim i As Integer

lastRow = Cells(Rows.Count, 2).End(xlUp).Row
i = 0

If lastRow > 2 Then
    Set rng = ActiveSheet.Range("B3:B" & lastRow)
    Debug.Print "Number of empty cells: " & rng.SpecialCells(xlCellTypeBlanks).Count
    For Each rw In rng.SpecialCells(xlCellTypeBlanks).Cells
        Rows(rw.Row).Delete shift:=xlUp
        i = 1 + i
    Next rw
    Debug.Print "Number of empty rows deleted: " & i
End If


Thanks!


Dennis
 
Better do like this
VBA Code:
    Dim i           As Long

    Dim lastRow     As Long
    lastRow = Cells(Rows.Count, 2).End(xlUp).Row
    Application.ScreenUpdating = False

    For i = lastRow To 3 Step -1   ' Let's go from the bottom up
        If IsEmpty(Cells(i, 2)) Then Rows(i).Delete shift:=xlUp
    Next i

    Application.ScreenUpdating = True
 
Upvote 0

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