code for hiding unhiding rows

chobanne

Active Member
Joined
Jul 3, 2011
Messages
269
Can someone please help me to make this code for my sheet.

I have cell A1 which is actually data validation list. When i choose some value in that cell, the values in column B changes in a way that some values becomes 0 some becomes 1. Thats are formula connection values in column B

I need a code that will hide me every rows with value 1 in colum B and unhide me every rows with value 0 in column B, every single time when i choose another value in A1.

Actually i can do this with filter command but when i choose another value in cell A1 i must do manually filtering again, and again etc.

Thank you
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this code in teh VBG Editor window that pops up and see if that does what you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lr As Long, r As Long
    
'   Exit it cell A1 not updated
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    
'   Find last row with values in column B
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
'   Loop through all rows
    For r = 1 To lr
        If Cells(r, "B").Value = 0 And Len(Cells(r, "B")) = 1 Then
            Rows(r).EntireRow.Hidden = False
        Else
            If Cells(r, "B").Value = 1 Then
                Rows(r).EntireRow.Hidden = True
            End If
        End If
    Next r

    Application.ScreenUpdating = True

End Sub
It should run every time you update the value in cell A1.
 
Upvote 1
Solution
Excellent!
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,215,145
Messages
6,123,291
Members
449,094
Latest member
GoToLeep

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