VBA code to automatically hide rows

smathu3

New Member
Joined
Jun 13, 2016
Messages
11
I want to find a way to automatically hide rows in Excel when any particular cell is 0 within a specified column. In the example below, after running the macro, the column on top would collapse and then it would look like the column on the bottom (since the cells with 0 would have their rows be automatically hidden).

0
400
0
0
300
150
0
0
0
400

<tbody>
</tbody>

400
300
150
400

<tbody>
</tbody>
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
.
This is one way of many :

Code:
Option Explicit


Sub HidebyVal()
Dim i As Integer
Application.ScreenUpdating = False
    For i = 2 To 300
        If Cells(i, 4).Value = "0" Then
            Cells(i, 4).EntireRow.Hidden = True
        End If
       
    Next
Application.ScreenUpdating = True
End Sub


Sub UnHide()
Dim i As Integer
Application.ScreenUpdating = False


'MsgBox "Unhiding"
    For i = 2 To 300
        If Cells(i, 4).Value = "0" Then
            Cells(i, 4).EntireRow.Hidden = False
        End If
    Next i
Application.ScreenUpdating = True


End Sub
 
Upvote 0
There are a few subtle differences between Hidden and Filtered.
Logit gave what you asked for, but I loath hidden rows because of the way they are treated with SUBTOTAL, AGGREGATE and other functions. Also, with a Filtered set, I can Copy/Paste without worrying about the Visible Cells Only.
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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