VBA Worksheet visibility when equal to cell range

BrainDiseasee

New Member
Joined
Aug 30, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am working with a large workbook where a roster of people have a worksheet for every name. The goal is that i can divide this roster up in to teams or different ranges for each team. When a macro is activated it only shows the ws names that are equal the specified range in the selected macro. I have done something similar to this to create WS based on a a range of cells but i keep getting a mismatch error. Below is just a sample of the code i am trying to work with. Once again the goal is when the macro is activated to change the sheet visibilities to TRUE for all sheet names found in the range N2.END(xlDown)

Thanks in advance to anyone that can help.

Sub sheet_Vis()

Dim NameField As Range
Dim Mgr As Range

Dim WSheet As Worksheet
Dim WSheetFound As Boolean
Dim DataWSheet As Worksheet


Set DataWSheet = Worksheets("Data")
Set NameField = DataWSheet.Range("N2", DataWSheet.Range("N2").End(xlDown))

Application.ScreenUpdating = False

For Each Mgr In NameField

For Each WSheet In ThisWorkbook.Worksheets
If WSheet.Name = NameField Then
WSheetFound = True
WSheet.Visible = True
Exit For
Else
WSheetFound = False
WSheet.Visible = False

Application.ScreenUpdating = True

End If

Next WSheet

Next


End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
While i think i have solved the previous issue i am now working on the opposite problem If i have multiple ranges i want to use to control the visibility too i need to make the visibility FALSE for the sheets not equal to my range. Below is the current code i used to control what sheet names are visible when the sheet name is in the range. Needing to make visibility false for all other sheets not equal to the range.


(PS i think this is sloppy and if there is a better way please let me know)

Sub Sheet_Vis()

Dim cell As Excel.Range
Dim sh As Worksheet

For Each cell In Worksheets("Data").Range("N2:N" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)

For Each sh In Worksheets
If sh.Name = cell.Value Then
sh.Visible = True

End If

Next
Next
End Sub
 
Upvote 0
Test this on a copy of your Workbook as unexpected results may occur. Place this code in ThisWorkbook of your VBA Project Model.
VBA Code:
Sub Sheet_Vis()

Dim cell As Range, sht As Worksheet, rng As Range, lRow As Long, ws As Worksheet, wb As Workbook
Set wb = ThisWorkbook: Set sht = wb.Sheets("Data")
lRow = sht.Columns("N").Rows(sht.Rows.Count).End(xlUp).Row
Set rng = sht.Range("N2:N" & lRow)
Application.ScreenUpdating = False
For Each ws In wb.Sheets
    ws.Visible = xlSheetHidden
        For Each cell In rng
            If ws.Name = cell.Value Then
                ws.Visible = xlSheetVisible
                Exit For
            End If
        Next cell
Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Test this on a copy of your Workbook as unexpected results may occur. Place this code in ThisWorkbook of your VBA Project Model.
VBA Code:
Sub Sheet_Vis()

Dim cell As Range, sht As Worksheet, rng As Range, lRow As Long, ws As Worksheet, wb As Workbook
Set wb = ThisWorkbook: Set sht = wb.Sheets("Data")
lRow = sht.Columns("N").Rows(sht.Rows.Count).End(xlUp).Row
Set rng = sht.Range("N2:N" & lRow)
Application.ScreenUpdating = False
For Each ws In wb.Sheets
    ws.Visible = xlSheetHidden
        For Each cell In rng
            If ws.Name = cell.Value Then
                ws.Visible = xlSheetVisible
                Exit For
            End If
        Next cell
Next ws
Application.ScreenUpdating = True
End Sub
This works great! Except i have realized i have not made this dynamic enough. How could i make this a run every time the cell value of N1 Changes?

Thanks in advance!
 
Upvote 0
Place this in the "Data" Sheet of you VBA Project Model.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Me.Range("N1") Then
    ThisWorkbook.Sheet_Vis
End If
End Sub
 
Upvote 0
Place this in the "Data" Sheet of you VBA Project Model.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Me.Range("N1") Then
    ThisWorkbook.Sheet_Vis
End If
End Sub
Thanks again, i actually got that one figured out but i appreciate your help!
 
Upvote 0
This works great!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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