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).

[TABLE="class: grid, width: 50, align: left"]
<tbody>[TR]
[TD]0[/TD]
[/TR]
[TR]
[TD]400[/TD]
[/TR]
[TR]
[TD]0[/TD]
[/TR]
[TR]
[TD]0[/TD]
[/TR]
[TR]
[TD]300[/TD]
[/TR]
[TR]
[TD]150[/TD]
[/TR]
[TR]
[TD]0[/TD]
[/TR]
[TR]
[TD]0[/TD]
[/TR]
[TR]
[TD]0[/TD]
[/TR]
[TR]
[TD]400[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="class: grid, width: 50"]
<tbody>[TR]
[TD]400[/TD]
[/TR]
[TR]
[TD]300[/TD]
[/TR]
[TR]
[TD]150[/TD]
[/TR]
[TR]
[TD]400[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
.
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,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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