I found this code on the net, and supposedly it works, and you would have to do a loop to unhide them, but I’m trying to hide them. So, why doesn’t this work. And, do I just drop the dimensions of the Listboxes in willy-nilly, or do they need a With Statement or something?
I’ve tried everything I can think of
Private Sub Workbook_Open()
Worksheets(Array("Data", "CLLIDATA", "OXXXXS", "LXXXXXXB", "LXXXXXXT", "BXXXXXXO", _
"LXXXXXXO", "NXXXXXXV", "NXXXXXX1", "JXXXXXXX", "NXXXXXXP", "LXXXXXXB-LXXXXXXT MAP", _
"LXXXXXXB-BXXXXXXO MAP", "LXXXXXXB-LXXXXXXO MAP")).Visible = False
With ThisWorkbook.Sheets("Index").ListBox1
.Clear
.List = ThisWorkbook.Sheets("Data").Range("F1:F15").Value
End With
With ThisWorkbook.Sheets("Index").ListBox2
.Clear
.List = ThisWorkbook.Sheets("Data").Range("D1:D8").Value
End With
Sheets("Index").ListBox1.Height = 141
Sheets("Index").ListBox1.Left = 333.5
Sheets("Index").ListBox1.Top = 102
Sheets("Index").ListBox1.Width = 195.5
Sheets("Index").ListBox2.Height = 144
Sheets("Index").ListBox2.Left = 128.5
Sheets("Index").ListBox2.Top = 20
Sheets("Index").ListBox2.Width = 82.5
Sheets("Index").ListBox3.Height = 152
Sheets("Index").ListBox3.Left = 650
Sheets("Index").ListBox3.Top = 20
Sheets("Index").ListBox3.Width = 87.5
Sheets("Index").ToggleButton1.Height = 28.5
Sheets("Index").ToggleButton1.Left = 384.5
Sheets("Index").ToggleButton1.Top = 17.5
Sheets("Index").ToggleButton1.Width = 93.5
Sheets("Index").TextBox1.Height = 29
Sheets("Index").TextBox1.Left = 361
Sheets("Index").TextBox1.Top = 60
Sheets("Index").TextBox1.Width = 140.5
End Sub
4
I created a subroutine specifically to set the visibility of the sheets array. This is preferable because we can test it independently of the main code. We can also unhide the same group of sheets if we like.
Private Sub Workbook_Open()
SetVisibility_ArrayOfWorksheet xlSheetHidden
With ThisWorkbook.Sheets("Index").ListBox1
.Clear
.List = ThisWorkbook.Sheets("Data").Range("F1:F15").Value
End With
With ThisWorkbook.Sheets("Index").ListBox2
.Clear
.List = ThisWorkbook.Sheets("Data").Range("D1:D8").Value
End With
Sheets("Index").ListBox1.Height = 141
Sheets("Index").ListBox1.Left = 333.5
Sheets("Index").ListBox1.Top = 102
Sheets("Index").ListBox1.Width = 195.5
Sheets("Index").ListBox2.Height = 144
Sheets("Index").ListBox2.Left = 128.5
Sheets("Index").ListBox2.Top = 20
Sheets("Index").ListBox2.Width = 82.5
Sheets("Index").ListBox3.Height = 152
Sheets("Index").ListBox3.Left = 650
Sheets("Index").ListBox3.Top = 20
Sheets("Index").ListBox3.Width = 87.5
Sheets("Index").ToggleButton1.Height = 28.5
Sheets("Index").ToggleButton1.Left = 384.5
Sheets("Index").ToggleButton1.Top = 17.5
Sheets("Index").ToggleButton1.Width = 93.5
Sheets("Index").TextBox1.Height = 29
Sheets("Index").TextBox1.Left = 361
Sheets("Index").TextBox1.Top = 60
Sheets("Index").TextBox1.Width = 140.5
End Sub
In a public module:
Sub SetVisibility_ArrayOfWorksheet(Visible As XlSheetVisibility)
' Declare a variable to hold the target worksheets
Dim TargetWorksheets As Sheets
' Set the target worksheets to an array of sheet names
' The array contains the names of multiple worksheets that need visibility changes
Set TargetWorksheets = Worksheets(Array("Data", "CLLIDATA", "OXXXXS", _
"LXXXXXXB", "LXXXXXXT", "BXXXXXXO", "LXXXXXXO", "NXXXXXXV", "NXXXXXX1", _
"JXXXXXXX", "NXXXXXXP", "LXXXXXXB-LXXXXXXT MAP", _
"LXXXXXXB-BXXXXXXO MAP", "LXXXXXXB-LXXXXXXO MAP"))
' Declare a variable to iterate over each worksheet
Dim ws As Worksheet
' Loop through each worksheet in the TargetWorksheets collection
For Each ws In TargetWorksheets
' Set the visibility of each worksheet to the value provided (Visible)
' Visible can be xlSheetVisible, xlSheetHidden, or xlSheetVeryHidden
ws.Visible = Visible
Next
End Sub
0
I couldn’t get your method to work for some reason. I got this to work:
Dim sheetNamesToHide As Variant
sheetNamesToHide = Array("Data", "CXXXXXXA", "OXXXXXXS", "LXXXXXXB", "LXXXXXXT", _
"BXXXXXXO", "LXXXXXXO", "NXXXXXXV", "NXXXXXX1", "JXXXXXXX", "NXXXXXXP", _
"LXXXXXXB-LXXXXXXT MAP", "LXXXXXXB-BXXXXXXO MAP", "LXXXXXXB-LXXXXXXO MAP")
For Each Loop
'Check if the sheet name is in the list to hide
If IsError(Application.Match(ws.Name, sheetNamesToHide, 0)) Then
'Sheet names not found in the list, so not hidden
Else
ws.Visible = xlSheetHidden
End If