Search code examples
vbaexcelhide

Hide and Unhide Rows via Single Button


I have looked through a couple of the questions that have previously been posted relating to this topic and believe someone on this community will be able to assist with my inquiry. I am fairly novice to VBA so I apologize in advance if my question is elementary.

My goal is to use a single macro tied to a button to Hide and then Un-hide a selected number of rows (these rows will always be the same) after clicking the button again. I want each of these actions to be completed by separate, solitary clicks without utilizing any Message Boxes or timed delays. I've recorded a macro with the actions & their criteria and pasted it below. The only missing piece is the code needed to separate these actions before being clicking the button again and running the next action. Thanks in advance for the assistance.

Sub Macro1()    
    Rows("7:21").Select  
    Selection.EntireRow.Hidden = True    
    Rows("7:21").Select   
    Selection.EntireRow.Hidden = False
End Sub

Solution

  • Try something similar to this:

    Sub Macro1()
    
    ' Macro1 Macro ' '
    Rows("7:21").Select
    
    If Rows("7:21").Hidden = True Then
        Selection.EntireRow.Hidden = False
    Else
        Selection.EntireRow.Hidden = True
    End If
    
    End Sub
    

    All that is left to do is attach it to a button.