Delete rows that contain highlighting

Serafin54

Board Regular
Joined
Apr 11, 2014
Messages
165
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have this vba code that does the opposite of what i need. It currently deletes any rows that does not contain highlighting but need it do do the opposite. It had Colorindex = 19 which deleted all the rows not highlighted so I changed the index color to -4142 and it runs without error but doesn't do anything. hoping someone could point me to what I am missing.

VBA Code:
Sub ShadedRow()
    Dim cel As Range, rng As Range, uRng As Range, c As Long, R As Long
    Set rng = ActiveSheet.UsedRange
    Set uRng = rng.Offset(rng.Rows.Count).Resize(1, 1)
 
    For R = 2 To rng.Rows.Count
        For c = 1 To rng.Columns.Count
            Set cel = rng.Cells(R, c)
            If Not cel.EntireRow.Interior.ColorIndex = -4142 Then
                Set uRng = Union(uRng, cel)
                Exit For
            End If
        Next c
    Next R
    uRng.EntireRow.Delete
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I think -4241 represents the default setting for cell background and any number greater than zero would represent a colour. So maybe delete where the colorindex > 0 ? Step through your code first to be sure it executes as you expect and that the variables contain the values you expect.
 
Upvote 0
I think -4241 represents the default setting for cell background and any number greater than zero would represent a colour. So maybe delete where the colorindex > 0 ? Step through your code first to be sure it executes as you expect and that the variables contain the values you expect.
Think I got it after a lot of trial and error.

If Not cel.Interior.ColorIndex = x1None

Thanks for the suggestion
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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