Hide and Unhide rows with single macro

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a table listed below and I would like to add a button to the blue/maroon colored rows that would hide or unhide the rows in between the blue/maroon colored rows. I would like to have one macro that would work throughout the whole worksheet. If rows are visible, hide them and if they are hidden, show them. I have attempted to start a macro below, but I cannot get it to work. Is there a cleaner or more straightforward way to get this to work?

Thanks for the help.

Macro

VBA Code:
Sub SHOW_HIDE_ROWS()

Application.EnableEvents = False

If ActiveCell.Interior.Color = 15773696 Or ActiveCell.Interior.Color = RGB(192, 0, 0) And ActiveCell.Offset(1, 0).EntireRow.Hidden = True Then
    Do Until ActiveCell.Interior.Color = 15773696 Or ActiveCell.Interior.Color = RGB(192, 0, 0)
        ActiveCell.Offset(1, 0).Select
        ActiveCell.EntireRow.Hidden = False
    Loop
Else
    Do Until ActiveCell.Interior.Color = 15773696 Or ActiveCell.Interior.Color = RGB(192, 0, 0)
        ActiveCell.Offset(1, 0).Select
        If ActiveCell.Interior.Color <> 15773696 Or ActiveCell.Interior.Color <> RGB(192, 0, 0) Then
            ActiveCell.EntireRow.Hidden = True
        End If
        
    Loop
End If

Application.EnableEvents = True

End Sub

Table

Book1
ABCDEFGHI
1Fruit
2Oranges2550224834661474
3Apples1015156055351824
4Grapes155974846882327
5Meat
6Bacon345689945116518
7Chicken1015156055351824
8Port Roast6658411654889738
9Beef Steak155974846882327
10Pot Roast2451546638323041
11Breads
12French Bread718221816312327
13Wheat bread1015156055351824
14Sourdough155974846882327
15White bread5437386849841813
16Rye bread1015156055351824
17Multigrain325654897356415
18Lemon Poppy seed155974846882327
19Produce
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C18:I18Expression=MOD(COLUMN(),2)=0textNO
C14:I14Expression=MOD(COLUMN(),2)=0textNO
C9:I9Expression=MOD(COLUMN(),2)=0textNO
C16:I16Expression=MOD(COLUMN(),2)=0textNO
C13:I13Expression=MOD(COLUMN(),2)=0textNO
C7:I7Expression=MOD(COLUMN(),2)=0textNO
C12:I12,C6:I6,C2:I4,C8:I8,C15:I15,C17:I17,C10:I10Expression=MOD(COLUMN(),2)=0textNO
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
VBA Code:
Sub SHOW_HIDE_ROWS()
    Dim lastrow As Long, x As Long
    Application.EnableEvents = False
    lastrow = ActiveSheet.Range("A:A").Find(What:="*", After:=ActiveSheet.Range("A1"), LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 1 To lastrow
        With Range("A" & x)
        If Not (.Interior.Color = 15773696 Or .Interior.Color = RGB(192, 0, 0)) Then .EntireRow.Hidden = IIf(.EntireRow.Hidden = False, True, False)
        End With
    Next
    Application.EnableEvents = True
End Sub
 
Upvote 0
You could simplify that slightly like
VBA Code:
        If Not (.Interior.Color = 15773696 Or .Interior.Color = RGB(192, 0, 0)) Then .EntireRow.Hidden = Not .EntireRow.Hidden
 
Upvote 0
Thank you for the quick response. You code works to hide all the rows on the whole sheet great. I was not clear in my description of what I wanted the macro to do. I only want the macro to hide the cells in between only one block of colored rows and then stop. This way if the user wants to see one section and hide another they can just click on the button in each colored row and it will either show or hide the rows in that block. In my table example, if the user click on the button in row 5 (Meat) rows 6-10 would be hidden. If the user selects row five again, the cells would show.
 
Upvote 0
Thank you all for your help, with a little experimentation using the code already supplied by Fluff and Mart37, coupled with a lot of luck on my end, I managed to get what I need done. The members and support from this website is awesome. Thank you all.
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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