multiple-row hide and unhide depending on cell

alan myers

Board Regular
Joined
Oct 31, 2017
Messages
119
Office Version
  1. 365
Platform
  1. Windows
Column AColumn BColumn CColumn D
Row 1CarriersHide Y/NStartEnd
Row 2Carrier1Y351
Row 3Carrier2Y6068
Row 4Carrier3n7079
Row 5Carrier4n8486
Row 6Carrier5n8791
Row 7Carrier6Y9299
Row 8Carrier7Y100105
Row 9Carrier8n110150
Row 10Carrier9n200225
Row 11Carrier10Y250275
Row 12Carrier11Y300340
-
-
-
-
Row 196Carrier 195n12341300





I need VBA code that when I change starting at b2 to a y, it hides the rows from c2 value to d2 value, and when I switch back to an n, it unhides them. There will be 195 cells that need to be checked.
the show hide tab is where I change b2 and I need to hide the rows on the cover sheet tab.

here is my file https://www.mediafire.com/file/aq3h8yk8dqex04n/Payroll+v1a.xlsm/file
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Go to the sheet with this grid.
Then tight-click on the sheet tab name at the bottom of your screen, select "View Code", and paste this code in the resulting VB Editor:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim fr As Long
    Dim lr As Long
    
'   See if cell in cell B manually updated
    Set rng = Intersect(Target, Range("B:B"))
    If rng Is Nothing Then Exit Sub
    
'   Loop through updated cells in column B
    For Each cell In rng
'       Capture rows to update
        fr = Cells(cell.Row, "C").Value
        lr = Cells(cell.Row, "D").Value
'       Determine to hide or unhide rows
        Select Case UCase(cell)
            Case "Y"
                Sheets("Cover Sheet").Rows(fr & ":" & lr).Hidden = True
            Case "N"
                Sheets("Cover Sheet").Rows(fr & ":" & lr).Hidden = False
        End Select
    Next cell

End Sub
This should work automatically as you toggle the values in column B for "Y" to "N" and vice versa.
Note that I did not do this off of your file, which I cannot download from my current location. I did this based off of your description.
So change the sheet name in the code, if necessary.
 
Upvote 0
Solution
One more option.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With ActiveSheet
        If UCase(Intersect(Target, .Columns("B")).Value) = UCase("y") Then
            .Rows(Target.Offset(0, 1) & ":" & Target.Offset(0, 2)).Hidden = 1
        End If
        If Not (UCase(Intersect(Target, .Columns("B")).Value)) = UCase("y") Then
            .Rows(Target.Offset(0, 1) & ":" & Target.Offset(0, 2)).Hidden = 0
        End If
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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