VBA - Hide Rows (Named Range) ALL worksheets, based on one cell reference

JSpaced

New Member
Joined
Aug 11, 2015
Messages
5
Hi guys,
I've been doing some googling and searching on this forum and I can't quite find the answer I need.
So here's the situation:
I have a spreadsheet of students in tutor groups (about 15 students).
First worksheet is the summary sheet and gathers together data based on the data in the individual student worksheets.
Each student worksheet has a daily record: a grid of scores each day over an 8 week period.

I have named ranges on each student worksheet (week1, week2, etc.)

Up until now I've used the following code, operated by a click button on each student sheet to hide everything except the current week for data entry:

Code:
Sub ShowWeek1()
If InStr(1, ActiveSheet.Range("J2").Value, "1") > 0 Then
    Range("Week1").EntireRow.Hidden = False
Else
    Range("Week1").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "2") > 0 Then
    Range("Week2").EntireRow.Hidden = False
Else
    Range("Week2").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "3") > 0 Then
    Range("Week3").EntireRow.Hidden = False
Else
    Range("Week3").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "4") > 0 Then
    Range("Week4").EntireRow.Hidden = False
Else
    Range("Week4").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "5") > 0 Then
    Range("Week5").EntireRow.Hidden = False
Else
    Range("Week5").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "6") > 0 Then
    Range("Week6").EntireRow.Hidden = False
Else
    Range("Week6").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "7") > 0 Then
    Range("Week7").EntireRow.Hidden = False
Else
    Range("Week7").EntireRow.Hidden = True
End If

If InStr(1, ActiveSheet.Range("J2").Value, "8") > 0 Then
    Range("Week8").EntireRow.Hidden = False
Else
    Range("Week8").EntireRow.Hidden = True
End If

End Sub

The complaint from teachers using this is that they get confused if they have to keep clicking the "Show" button on each sheet in turn.

I've been struggling to find a way to make one button, on the first sheet, operate the macro to hide the appropriate rows on all sheets in the workbook.

Does anyone have any ideas?

Cheers,
J
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
If J2 on a student worksheet is blank then it will use the value of J@ from the summary worksheet

Code:
Option Explicit

Sub ShowWeekBasedOnJ2ValueOfEachWorksheet()

    Dim lWeekIndex As Long
    Dim wks As Worksheet
    Dim lWeekValue As Long
    
    For Each wks In ThisWorkbook.Worksheets
        Select Case wks.Name
        Case "Summary", "Contact"  'Enter names of all non-student sheets here
            'Do nothing
        Case Else
            'Remaining wks should be student sheets
            
            'If the week to show on wks is blank, get the value from the summary sheet
            If wks.Range("J2").Value = "" Then
                lWeekValue = Worksheets("Summary").Range("J2").Value
            Else
                lWeekValue = wks.Range("J2").Value
            End If
            
            'Hide/Unhide ranges on wks
            For lWeekIndex = 1 To 8
                If InStr(1, lWeekValue, lWeekIndex) > 0 Then
                    wks.Range("Week" & lWeekIndex).EntireRow.Hidden = False
                Else
                    wks.Range("Week" & lWeekIndex).EntireRow.Hidden = True
                End If
            Next
        End Select
    Next
End Sub
 
Upvote 0
Wow, Phil, that's incredible. The J2 cell in each worksheet pulls from the "WeekNo" named cell on the Setup sheet, so it will always be populated (in theory).
I'm going to experiment with this, but does it go into the VBA for each sheet, or just the workbook?
Many thanks,
J
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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