Hide and unhide rows

Moluccanmom

New Member
Joined
Oct 13, 2016
Messages
41
Hello,<o:p></o:p>
<o:p></o:p>
I have a button that hides rows with value 0 incolumn A.<o:p></o:p>
<o:p></o:p>
Code:
[SIZE=3][COLOR=#000000][FONT=Calibri]Sub HideRows()<o:p></o:p>[/FONT][/COLOR][/SIZE]
<o:p></o:p>
[SIZE=3][COLOR=#000000][FONT=Calibri]LastRow = 400 <o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]For i = 1 To LastRow <o:p></o:p>[/FONT][/COLOR][/SIZE]
<o:p></o:p>
[SIZE=3][COLOR=#000000][FONT=Calibri]If Range("A" & i) = 0 Then Rows(i).EntireRow.Hidden = True<o:p></o:p>[/FONT][/COLOR][/SIZE]
[SIZE=3][COLOR=#000000][FONT=Calibri]Next<o:p></o:p>[/FONT][/COLOR][/SIZE]
<o:p></o:p>
[SIZE=3][COLOR=#000000][FONT=Calibri]End Sub[/FONT][/COLOR][/SIZE]
<o:p></o:p>
<o:p></o:p>
Is there a way to unhide all rows by clicking the button a second time?<o:p></o:p>
<o:p></o:p>
Thank you!<o:p></o:p>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
One way is to use some unused cell on your sheet (like AZ1) to store whether or not the rows are hidden/unhidden, and then use an IF statement.
Something like:
Code:
Sub HideRows()

    Dim LastRow As Long
    Dim i As Long
    Dim Status As String
    
    LastRow = 400
    
'   Get status from cell AZ1
    Status = Range("AZ1")
    
'   Check status
    If Status = "Hidden" Then
'       Unhide all rows
        Rows("1:" & LastRow).Hidden = False
'       Update status
        Range("AZ1") = "Unhidden"
    Else
'       Go through hide process
        For i = 1 To LastRow
            If Range("A" & i) = 0 Then Rows(i).EntireRow.Hidden = True
        Next i
'       Update status
        Range("AZ1") = "Hidden"
    End If
    
End Sub
 
Upvote 0
You are welcome!
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

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