Hide/filter rows or columns based on selected cell color

pholt33

Board Regular
Joined
Jan 4, 2005
Messages
202
Office Version
  1. 365
Platform
  1. Windows
Excel's built-in "filter by selected cell color" is really helpful but also limited. How can I do the opposite... filter out or hide rows based on the selected cell color. I found the below in an old post on filtering based on a specific color. Is there a way to modify that or is there a different code that can do what I am asking?


VBA Code:
Option Explicit

Sub test()
Dim LR As Long      'Last Row
Dim RN As Integer   'Row Number

LR = Cells(Rows.Count, "J").End(xlUp).Row

Application.ScreenUpdating = False

    For RN = 1 To LR
    If Cells(RN, "J").Interior.ColorIndex = 6 Then Cells(RN, "J").EntireRow.Hidden = True
    Next RN
    
 Application.ScreenUpdating = True
 
End Sub

I modified the above to <> 6 and that worked fine. Then I tried this without success:
VBA Code:
    For RN = 1 To LR
        If Cells(RN, "A").Interior.ColorIndex = ActiveCell.Interior.Color Then _
                Cells(RN, "A").EntireRow.Hidden = True
    Next RN

Also, the above is specific to J:J, how can I make it work for any column?

Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Like this:
VBA Code:
Public Sub Test()

    Dim LR As Long 'last Row
    Dim LC As Long 'last column
    Dim R As Long, C As Long
    
    LR = Cells(Rows.Count, "J").End(xlUp).Row
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Application.ScreenUpdating = False
    
    For R = 1 To LR
        For C = 1 To LC
            If Cells(R, C).Interior.ColorIndex = ActiveCell.Interior.ColorIndex Then
                Cells(R, C).EntireRow.Hidden = True
                Exit For
            End If
        Next
    Next
    
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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