VBA For Each Loop Deleting Rows

beartooth91

Board Regular
Joined
Dec 15, 2024
Messages
66
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I'm pretty sure this is a simple oversight, but I can't seem to figure it out:
I have a worksheet with data from B11:BO and to about row 100 (though it varies)
The code below is supposed to loop through and delete rows with the highlighted B cell color.
It kind of works....meaning when I run it; it deletes some of them and it takes me running the procedure another 3-4 times to get all of them.

VBA Code:
Sub Delete_NA_Points()
'
'Define Variables----------------------------
Dim lr As Long, cell As Range
'--------------------------------------------


With Sheets(1)
  lr = .Cells(Rows.Count, "B").End(xlUp).Row
 
  For Each cell In .Range("B11:B" & lr)
    If cell.Interior.ColorIndex = 22 Then
      cell.EntireRow.Delete
    End If
  Next cell
End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
When you are deleting rows, you are changing the size of the range as you are trying to loop through it, which can cause rows to be missed.
Whenever deleting or inserting rows, you should always start from the bottom and work your way up, like this:
VBA Code:
Sub Delete_NA_Points()
'
'Define Variables----------------------------
Dim lr As Long, r As Long
'--------------------------------------------


With Sheets(1)
  lr = .Cells(Rows.Count, "B").End(xlUp).Row
 
  For r = lr To 11 Step -1
    If Cells(r, "B").Interior.ColorIndex = 22 Then
      Rows(r).Delete
    End If
  Next r
End With
End Sub
 
Upvote 0
Solution
When deleting rows, you have to start the loop at the bottom of the data and work upwards. Try
VBA Code:
Sub Delete_NA_Points()
    Application.ScreenUpdating = False
    Dim lr As Long, x As Long
    With Sheets(1)
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        For x = lr To 11 Step -1
          If Range("B" & x).Interior.ColorIndex = 22 Then
            Rows(x).EntireRow.Delete
          End If
        Next x
    End With
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
When deleting rows, you have to start the loop at the bottom of the data and work upwards. Try
VBA Code:
Sub Delete_NA_Points()
    Application.ScreenUpdating = False
    Dim lr As Long, x As Long
    With Sheets(1)
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        For x = lr To 11 Step -1
          If Range("B" & x).Interior.ColorIndex = 22 Then
            Rows(x).EntireRow.Delete
          End If
        Next x
    End With
    Application.ScreenUpdating = False
End Sub
If referencing Rows instead of Cells, the "EntireRow" is redunant and unnecessary, ;)

So this:
VBA Code:
Rows(x).EntireRow.Delete
can just be simplified to this, like I did in my code:
VBA Code:
Rows(x).Delete
 
Upvote 0
Hi
another approach you could consider that negates deleting each row one by one

VBA Code:
Sub Delete_NA_Points()
    '
    'Define Variables----------------------------
    Dim lr          As Long
    Dim cell        As Range, DeleteRow As Range
    '--------------------------------------------
    Application.ScreenUpdating = False
    With Sheets(1)
        lr = .Cells(.Rows.Count, "B").End(xlUp).Row
        For Each cell In .Range("B11:B" & lr)
            If cell.Interior.ColorIndex = 22 Then
                If DeleteRow Is Nothing Then
                    Set DeleteRow = cell
                Else
                    Set DeleteRow = Union(cell, DeleteRow)
                End If
            End If
        Next cell
    End With
    'delete all matches in one go
    If Not DeleteRow Is Nothing Then DeleteRow.EntireRow.Delete
End Sub

Its a tad more code but may prove faster over a large range.

Hope helpful

Dave
 
Upvote 0
Hello,

If you have a very long list, you might take advantage of using range.Autofilter with color criteria, to instantly delete all rows.

VBA Code:
Sub Delete_NA_Points()

  Dim colRng As Range
  Set colRng = Sheets(1).Cells(11, "B")
 
  colRng.AutoFilter 1, 8421631, xlFilterCellColor
  Range(colRng, colRng.End(xlDown)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
 
  If Sheets(1).AutoFilterMode Then Sheets(1).ShowAllData
End Sub
"8421631" being the color code for ColorIndex = 22
 
Upvote 0
Though probably not as fast as some of the other excellent options presented here, one thing that would help to speed up the process using the loop method you first used would be to suppress calculations and sheet updating until all the deletions are completed, like this:
VBA Code:
Sub Delete_NA_Points()
'
'Define Variables----------------------------
Dim lr As Long, r As Long
'--------------------------------------------

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

With Sheets(1)
  lr = .Cells(Rows.Count, "B").End(xlUp).Row
 
  For r = lr To 11 Step -1
    If Cells(r, "B").Interior.ColorIndex = 22 Then
      Rows(r).Delete
    End If
  Next r
End With

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0
When you are deleting rows, you are changing the size of the range as you are trying to loop through it, which can cause rows to be missed.
Whenever deleting or inserting rows, you should always start from the bottom and work your way up, like this:
VBA Code:
Sub Delete_NA_Points()
'
'Define Variables----------------------------
Dim lr As Long, r As Long
'--------------------------------------------


With Sheets(1)
  lr = .Cells(Rows.Count, "B").End(xlUp).Row
 
  For r = lr To 11 Step -1
    If Cells(r, "B").Interior.ColorIndex = 22 Then
      Rows(r).Delete
    End If
  Next r
End With
End Sub
This worked, Thanks!
 
Upvote 0
You are welcome.
Glad we were able to help.
Be sure to take a look at some of the other methods, some really good stuff there!
 
Upvote 0

Forum statistics

Threads
1,226,117
Messages
6,189,061
Members
453,524
Latest member
AshJames

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