Hide and Unhide Entire Rows Across Multiple Sheets Based on a Cell Value

jbusby825

New Member
Joined
Jan 27, 2016
Messages
25
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that has 54 sheets (a TOC, Setup, and a tab for 52 weeks).

In the Setup sheet I have 20 products setup and I have a Y/N column next to each product to determine if that product is being used or not. What I want is, if the product has an N next to it and it is not being used, then i want to hide all the rows associated with that product in the 52 weeks. If I come in and change the N to a Y, then I want it to unhide all the rows associated with the product in the 52 weeks.

You can see in the code what I have been testing out, it's inside the Setup worksheet. It works, but I don't want to be creating this line of code for 52 separate tabs and for 20 different products. What is a better solution?

Thanks!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

' Hiding/Unhiding Product1
   If Range("B3").Value = "N" Then
        Sheet3.Rows("10:13").EntireRow.Hidden = True
        Sheet4.Rows("10:13").EntireRow.Hidden = True
    Else
        Sheet3.Rows("10:13").EntireRow.Hidden = False
        Sheet4.Rows("10:13").EntireRow.Hidden = False
    End If


' Hiding/Unhiding Product20
   If Range("B22").Value = "N" Then
        Sheet3.Rows("85:88").EntireRow.Hidden = True
        Sheet4.Rows("85:88").EntireRow.Hidden = True
    Else
        Sheet3.Rows("85:88").EntireRow.Hidden = False
        Sheet4.Rows("85:88").EntireRow.Hidden = False
    End If
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Am I correct to assume that if Range("B3") corresponds to Rows("10:13") then does Range("B4") correspond to Rows(14:17).

If that is correct, then I have Product 20 or Range("B22") corresponding to Rows("86:89"). Please show a couple of more Ranges and their corresponding Rows, so a pattern is developed.
 
Upvote 0
Does this get you any closer to what you want. I would put this code into a code module and trigger it with a CommandButton on the "Setup" page... Additionally, I assumed that where you showed Sheet3 and Sheet4, you were using those as a test instead of putting in your full 52 weeks of sheets/tabs.

Code:
Sub test()


    Dim wsSU As Worksheet: Set wsSU = Worksheets("Setup")
    Dim ct As Integer, i As Integer, frst As Integer, scnd As Integer
    
    For ct = 1 To ThisWorkbook.Worksheets.Count
        If Not Worksheets(ct).Name = "TOC" And Not Worksheets(ct).Name = "Setup" Then
            For i = 3 To 22
                frst = ((i * 3) - 2) + i: scnd = frst + 3
                If wsSU.Range("B" & i) = "N" Then Worksheets(ct).Rows(frst & ":" & scnd).EntireRow.Hidden = True
                If wsSU.Range("B" & i) = "Y" Then Worksheets(ct).Rows(frst & ":" & scnd).EntireRow.Hidden = False
             Next
        End If
    Next
    
End Sub

I hope this helps.
 
Upvote 0
This is perfect. Is it possible to do it without assigning it to a button? where it would just automatically happen based on the Y/N value?

Thank you!!!
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Ws As Worksheet
   Dim Rw As Long
   If Target.CountLarge > 1 Then Exit Sub
   If Not Target.Column = 2 Then Exit Sub
   For Each Ws In Worksheets
      If Not Ws.Name = "TOC" And Not Ws.Name = "Setup" Then
         Rw = ((Target.row * 3) - 2) + Target.row
         Ws.Rows(Rw).Resize(4).Hidden = Target.Value = "N"
      End If
   Next Ws
End Sub
 
Last edited:
Upvote 0
@Fluff

Nice.

I have a question... Why do the rows change from hidden to not hidden if the target changes From "N" to "Y".

igold
 
Upvote 0
The part in blue
Code:
Ws.Rows(Rw).Resize(4).Hidden = [COLOR=#0000ff]Target.Value = "N"[/COLOR]
Will return either True or False depending on the target value. So if the target=N that part returns True & so the rows are hidden
 
Upvote 0
@Fluff

That worked great!!! Thank you so much!

I don't understand one thing that is listed in that code :confused: but i appreciate it.

The only thing I noticed in it is that you can't mass change the Y/N column. You have to change them one at a time which is not a big deal, but wanted to point it out.

Thanks!!!
 
Upvote 0
Are you copy/pasting data into the sheet?
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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