Selecting Occupied Rows without Fill Color

MR3

Board Regular
Joined
Jun 10, 2008
Messages
175


this is what I am trying to do. I would like to highlight the entire row if and only if none of the cells within that row are Filled in with any color.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try the following macro, which needs to be placed in a regular module (Alt+F11 > Insert > Module > Copy/Paste > Alt+Q)...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] FoundRows [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] Rw [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] Cell [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] CellCount [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]

    [color=darkblue]With[/color] ActiveSheet.UsedRange
        [color=darkblue]For[/color] [color=darkblue]Each[/color] Rw [color=darkblue]In[/color] .Rows
            CellCount = Rw.Cells.Count
            [color=darkblue]For[/color] [color=darkblue]Each[/color] Cell [color=darkblue]In[/color] Rw.Cells
                Cnt = Cnt + 1
                [color=darkblue]If[/color] Cell.Interior.ColorIndex <> -4142 [color=darkblue]Then[/color]
                    [color=darkblue]Exit[/color] [color=darkblue]For[/color]
                [color=darkblue]Else[/color]
                    [color=darkblue]If[/color] Cnt = CellCount [color=darkblue]Then[/color]
                        [color=darkblue]If[/color] FoundRows [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
                            [color=darkblue]Set[/color] FoundRows = Rw
                        [color=darkblue]Else[/color]
                            [color=darkblue]Set[/color] FoundRows = Union(FoundRows, Rw)
                        [color=darkblue]End[/color] [color=darkblue]If[/color]
                    [color=darkblue]End[/color] [color=darkblue]If[/color]
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Next[/color] Cell
            Cnt = 0
        [color=darkblue]Next[/color] Rw
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]If[/color] [color=darkblue]Not[/color] FoundRows [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
        FoundRows.Select
    [color=darkblue]Else[/color]
        MsgBox "No rows found...", vbInformation
    [color=darkblue]End[/color] [color=darkblue]If[/color]
                    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Hi Domenic,

I tried the code and it just highlights all the cells. It doesn't select the rows that contain no fill colors. I can send you my xl file so you can try the code yourself to verify.

Here is the screenshot of the output:

 
Last edited:
Upvote 0
When I run the macro based on your sample data, A6:E7 gets highlighted. Do you have separate data on the same worksheet?
 
Upvote 0
Hmm the data that i posted is the only data that exists.
There are two conditional formats set for the range of data given.
1. highlight in YELLOW blank cells
2. highlight in RED cells over 545

can you post a screen shot of what you see on your end?
 
Upvote 0
In that case, we can test using the same logic as Conditional Formatting. Try replacing...

Code:
If Cell.Interior.ColorIndex <> -4142 Then

with

Code:
If Cell.Value = "" Or Cell.Value > 545 Then
 
Upvote 0
As per your request, here is the code with comments included...

Code:
[font=Verdana][color=green]'Force explicit declaration of variables[/color]
[color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=green]'Declare the variables[/color]
    [color=darkblue]Dim[/color] FoundRows [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] Rw [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] Cell [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] CellCount [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]

    [color=darkblue]With[/color] ActiveSheet.UsedRange
        [color=green]'Loop through each row within the used range[/color]
        [color=darkblue]For[/color] [color=darkblue]Each[/color] Rw [color=darkblue]In[/color] .Rows
            [color=green]'Count the number of cells in the current row[/color]
            CellCount = Rw.Cells.Count
            [color=green]'Loop through each cell within the current row[/color]
            [color=darkblue]For[/color] [color=darkblue]Each[/color] Cell [color=darkblue]In[/color] Rw.Cells
                [color=green]'Keep a count of each iteration[/color]
                Cnt = Cnt + 1
                [color=green]'If current cell is blank or has a value greater than 545, exit For[/color]
                [color=darkblue]If[/color] Cell.Value = "" [color=darkblue]Or[/color] Cell.Value > 545 [color=darkblue]Then[/color]
                    [color=darkblue]Exit[/color] [color=darkblue]For[/color]
                [color=darkblue]Else[/color]
                    [color=green]'If current cell is the last cell in the row, assign the row to an object variable[/color]
                    [color=darkblue]If[/color] Cnt = CellCount [color=darkblue]Then[/color]
                        [color=darkblue]If[/color] FoundRows [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
                            [color=darkblue]Set[/color] FoundRows = Rw
                        [color=darkblue]Else[/color]
                            [color=darkblue]Set[/color] FoundRows = Union(FoundRows, Rw)
                        [color=darkblue]End[/color] [color=darkblue]If[/color]
                    [color=darkblue]End[/color] [color=darkblue]If[/color]
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Next[/color] Cell
            [color=green]'Re-set the count to zero[/color]
            Cnt = 0
        [color=green]'Go to the next row within the used range[/color]
        [color=darkblue]Next[/color] Rw
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=green]'If rows have been assigned to the object variable FoundRows, select the rows.[/color]
    [color=green]'Otherwise, display a message alerting the user that no rows were found[/color]
    [color=darkblue]If[/color] [color=darkblue]Not[/color] FoundRows [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
        FoundRows.Select
    [color=darkblue]Else[/color]
        MsgBox "No rows found...", vbInformation
    [color=darkblue]End[/color] [color=darkblue]If[/color]
                    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,717
Members
452,939
Latest member
WCrawford

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