+ Reply to Thread
Results 1 to 11 of 11

Hide All Rows Not Containing a Certain Color

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    59

    Hide All Rows Not Containing a Certain Color

    We use conditional formatting to highlight several cells in a large worksheet. One of the colors used is the "light red fill..." which appears when "Highlight Cell Rules => Format cells that are less than..." is selected under Conditional Formatting.

    Starting at row 3, I want to hide any rows that do not contain this color.

    I see how the code in the last post of this thread can work to hide rows based on values but I cannot figure out how to change it to start at row 3 and how to get it to hide everything not containing that "light red fill" color.

    Thanks in advance for any help,

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Hide All Rows Not Containing a Certain Color

    What is the logic for the coloring. This is needed as we will use that same logic to hide the rows.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Hide All Rows Not Containing a Certain Color

    You can just apply Data filter to range and choose Filter by Color option.

    Assuming there are no other format. You can choose No Fill to show all items excluding light pink.

    0.JPG

    Edit: I misread your statement, you should choose the color to show the color.
    Last edited by CK76; 08-18-2017 at 10:31 AM. Reason: See Edit:
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Hide All Rows Not Containing a Certain Color

    You could try this which assumes your data is in Column A
    Option Explicit
    
    Sub hidenoncolor()
        Dim lr As Long, i As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        For i = 1 To lr
            If Range("A" & i).Interior.colorindex = -4142 Then
                Range("A" & i).EntireRow.Hidden = True
            End If
        Next i
    
    End Sub
    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button

  5. #5
    Registered User
    Join Date
    05-07-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    59

    Re: Hide All Rows Not Containing a Certain Color

    • Data is contained in columns B through BA
    • Conditional formatting is based on certain cells containing 0. But the conditional formatting does not apply to the entire worksheet. Just certain sections. That is why we want to use color to hide the rows.
    • Because the data is contained across several columns I cannot get the Data Filter to do it.
    Last edited by tgallag1; 08-18-2017 at 10:22 AM.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Hide All Rows Not Containing a Certain Color

    FYI - For color applied via Conditional format, you will need Excel 2010 or higher and use ".Range.DisplayFormat.Interior.ColorIndex".

    Do note that this property does not work as UDF when used in worksheet. Will work fine when used in sub.

    Otherwise, as alan stated, you will need underlying logic to detect the cells that has CF applied.

  7. #7
    Registered User
    Join Date
    05-07-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    59

    Re: Hide All Rows Not Containing a Certain Color

    OK I see now that doing it based on conditional formatting won't work. So how about this:

    For rows 5-100, only display rows containing 0. In other words hide all rows that do not contain a 0.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Hide All Rows Not Containing a Certain Color

    So should rows be hidden if any of columns B to BA don't contain 0?

    Or only if specific columns contain value other than 0?

    It would be helpful if you can upload sample.

  9. #9
    Registered User
    Join Date
    05-07-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    59

    Re: Hide All Rows Not Containing a Certain Color

    Thanks for the responses.

    Attached is a sample.

    If any cell in a given row, starting at row 5, from B to BA contains a 0, that row should be exposed.

    If, in a given row, there are no 0's, then that row should be hidden.

    So in the sample after running the code we would see (among others)rows 5, 7 and 9. But we would not see (among others) rows 41 and 43.
    Attached Files Attached Files

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Hide All Rows Not Containing a Certain Color

    try this:

    Option Explicit
    
    Sub HideRows()
        Application.ScreenUpdating = False
        Dim i As Long, x As Long
        Dim c As Range
        Dim rng As Range
        Dim lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        For i = 5 To lr
            x = 0
            Set rng = Range(Cells(i, "B"), Cells(i, "BA"))
            For Each c In rng
                If c <> 0 Then
                    x = x + 1
                    If x = 52 Then Range("A" & i).EntireRow.Hidden = True
                End If
            Next c
        Next i
        Application.ScreenUpdating = True
        MsgBox "Activity complete"
    
    End Sub

  11. #11
    Registered User
    Join Date
    05-07-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    59

    Re: Hide All Rows Not Containing a Certain Color

    Seems to work great. Thanks so much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need to hide cell + rows based on color
    By elmnas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-09-2015, 07:06 PM
  2. Hide rows that do not contain a certain color cell
    By ajzpop in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-01-2013, 10:36 AM
  3. [SOLVED] Macro to hide a series of rows that have no 'color' formatting
    By AJHughes in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-12-2012, 08:04 AM
  4. [SOLVED] Toggle button to hide rows if empty and has no color
    By hi2chris in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-10-2012, 08:56 PM
  5. Hide Rows based on cell color
    By Pergo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-02-2010, 03:08 PM
  6. Hide blank Rows - non text/color
    By jeff.p in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-29-2009, 03:25 PM
  7. Hide Rows based on cell color
    By jeff.p in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-26-2007, 10:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1