Refactor a VBA for deleting rows based on cell background color

thardy00

New Member
Joined
Jan 25, 2022
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi guys.
I'm just making my first steps into VBA and I don't know much yet. I've found a VBA online which deletes cells based on the background color and it works perfectly fine, but it could be tweaked a little bit. In my files I always have 4 headers rows and that VBA has no mercy for them :D. I would like to tune that VBA to make it start working from 4th row. Here's the code:
Excel Formula:
Sub DeleteRowsByColor()
    Dim rngCl As Range
    Dim xRows As Long
    Dim xCol As Long
    Dim colorLg As Long
    On Error Resume Next
    Set rngCl = Application.InputBox _
        (Prompt:="Select a cell with the background color to be deleted", _
        Title:="Kutools for Excel", Type:=8)
    On Error GoTo 0
    If rngCl Is Nothing Then
        MsgBox "User cancelled operation." & vbCrLf & _
        "Processing terminated", vbInformation, "Kutools for Excel"
        Exit Sub
    End If
    colorLg = rngCl.Interior.Color
    Application.ScreenUpdating = False
    With ActiveSheet.UsedRange
        For xRows = .Rows.Count To 1 Step -1
            For xCol = 1 To .Columns.Count
                           If .Cells(xRows, xCol).Interior.Color = colorLg Then
                    .Rows(xRows).Delete
                    Exit For
                End If
            Next xCol
        Next xRows
    End With
    Application.ScreenUpdating = True
End Sub

From my research I've realised that I need to change this line
VBA Code:
For xRows = .Rows.Count To 1 Step -1
, but I don't know how should I modify it to be honest.
Also I basing to on this VBA I've tried to make another one which will scroll the view to the next color in the given column, but my attempts to do that came to nothing. If someone will have some free time and good will I will really appreciate the help.

Best regards,
Tom.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Thanks for very fast reply!! Oh I feel super dumb right now haha, it was just a small tweak.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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