Delete colored rows but somehow it leaves some rows undeleted.

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello everyone.

I'm using below code to delete the colored rows (red).
The part that I don't understand is that it always leave a few rows undeleted.
Is it because the source row changes?
If someone can help me fix it, I'd really appreciate it.

PS: the rows is colored manually.



VBA Code:
Sub DeleteRedRows()

Dim sourceSheet As Worksheet
Dim sourceRow As Range





' Set the source and target sheets

Set sourceSheet = ThisWorkbook.Worksheets("NextPO") ' Replace "Sheet1" with the actual name of your source sheet




' Loop through each row in the source sheet

For Each sourceRow In sourceSheet.UsedRange.Rows

' Check if the background color of the row is red (RGB value: 65535)

If sourceRow.Interior.Color = RGB(255, 0, 0) Then sourceRow.EntireRow.Delete






 Next sourceRow



MsgBox "Red rows has been deleted !"

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
It does this because the code is deleting from the top down. Every time a row (sourcerow) gets deleted, they all shuffle up one, but the new row that replaces the deleted one has already been 'processed' so it gets left behind.
If you make a block of 10 red rows and look carefully you will find that each time the code runs it deletes half of those red rows.

To overcome this you need to delete from the bottom up.

VBA Code:
Sub DeleteRedRows()
    Dim sourceSheet As Worksheet
    Dim sourceRow As Range
    Dim lastrow As Long, i As Long
    '
    ' Set the source sheet
    '
    Set sourceSheet = ThisWorkbook.Worksheets("Sheet3") ' Replace "Sheet3" with the actual name of your source sheet
    ActiveSheet.UsedRange 'Refresh UsedRange
    lastrow = sourceSheet.UsedRange.Rows(sourceSheet.UsedRange.Rows.Count).Row
    ' Loop through each row in the source sheet
    For i = lastrow To 1 Step -1  ' Delete from the bottom up.
    ' Check if the background color of the row is red (RGB value: 65535)
        If Rows(i).EntireRow.Interior.Color = RGB(255, 0, 0) Then
            Rows(i).EntireRow.Delete
        End If
    Next i
    MsgBox "Red rows has been deleted !"
End Sub
 
Upvote 0
Solution
It does this because the code is deleting from the top down. Every time a row (sourcerow) gets deleted, they all shuffle up one, but the new row that replaces the deleted one has already been 'processed' so it gets left behind.
If you make a block of 10 red rows and look carefully you will find that each time the code runs it deletes half of those red rows.

To overcome this you need to delete from the bottom up.

VBA Code:
Sub DeleteRedRows()
    Dim sourceSheet As Worksheet
    Dim sourceRow As Range
    Dim lastrow As Long, i As Long
    '
    ' Set the source sheet
    '
    Set sourceSheet = ThisWorkbook.Worksheets("Sheet3") ' Replace "Sheet3" with the actual name of your source sheet
    ActiveSheet.UsedRange 'Refresh UsedRange
    lastrow = sourceSheet.UsedRange.Rows(sourceSheet.UsedRange.Rows.Count).Row
    ' Loop through each row in the source sheet
    For i = lastrow To 1 Step -1  ' Delete from the bottom up.
    ' Check if the background color of the row is red (RGB value: 65535)
        If Rows(i).EntireRow.Interior.Color = RGB(255, 0, 0) Then
            Rows(i).EntireRow.Delete
        End If
    Next i
    MsgBox "Red rows has been deleted !"
End Sub
Yes, you're right. It always leaves half of the red rows.
Thank you for your explanation and the solution.
I tried it and it works perfectly.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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