Hide/Unhide Rows Based on Cell Value

BruceJ

New Member
Joined
Jul 27, 2017
Messages
9
Hi All,

First post here. I'm an experienced Excel user (former Lotus user) and just beginning to attempt some VBA. Most of what I've learned (which isn't much yet) has come from this board. At this point, what I know is pretty much limited to copy/paste of what seems a reasonable match and editing as needed. Honestly, I don't really know enough yet to formulate coding from scratch.

That said, I know WHAT I want to do....just not HOW.

The worksheet I have has 2 sheets. Sheet1 feeds some data to Sheet2.

In Sheet2, Column F (a hidden column) is either 1 or blank and is populated from Sheet1. I want to Hide all rows in Sheet2 that do not have 1 in column F. The event should run at any change to Sheet1.

Thanks in advance for any help.

Bruce
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Filter would be a good option if it updated as the user made changes. The challenge with filter is that when the filter is applied, it does not update automatically as values in the filtered data changes.
 
Upvote 0
Hia
Try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With Sheets("Sheet2").Columns("F")
        .AutoFilter
        .AutoFilter field:=1, Criteria1:="=1", VisibleDropDown:=False
    End With

End Sub
This needs to be added to the sheet module.
Right click on sheet1 tab select view code & paste the above into the code window
 
Last edited:
Upvote 0
Hia
Try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With Sheets("Sheet2").Columns("F")
        .AutoFilter
        .AutoFilter field:=1, Criteria1:="=1", VisibleDropDown:=False
    End With

End Sub
This needs to be added to the sheet module.
Right click on sheet1 tab select view code & paste the above into the code window


Thanks Fluff! That works.

This is going to be used by a number of users. The downside I see in using Filter is that clicking the Filter button in the toolbar unfilters everything until the enter something in Sheet1 again. That's why I was thinking that Hide Row may be a solution to that. I'm not sure though.

I really appreciate your help.

Bruce
 
Upvote 0
Another option is
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim i As Long
    
    With Sheets("Sheet2")
        For i = 2 To .Range("F" & Rows.Count).End(xlUp).Row
            If .Range("F" & i) <> 1 Then .Rows(i).Hidden = True
        Next i
    End With

End Sub
But this could slow things down a lot if you have a lot of rows
 
Upvote 0
Hello Bruce
The downside I see in using Filter is that clicking the Filter button in the toolbar unfilters everything until the enter something in Sheet1 again.
Whilst this is true, there is nothing to actually show that a filter is in place. The column being filtered is hidden as is the filter button.
Also, with the second approach, there is nothing to stop a user from simply unhiding the hidden rows
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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