Sub test()
Dim ws As Worksheet, wsVar() As Variant, x As Long
For Each ws In Sheets
If Not ws.Visible Then
ReDim Preserve wsVar(x): wsVar(x) = ws.Name: x = x + 1
End If
Next ws
Range("A1").Resize(x) = Application.Transpose(wsVar)
End Sub
Thanks for your prompt reply. Yes, as you mentioned, the list should get changed when the visible sheet change to hidden or vice versa.The below will list them in column A:
VBA Code:Sub test() Dim ws As Worksheet, wsVar() As Variant, x As Long For Each ws In Sheets If Not ws.Visible Then ReDim Preserve wsVar(x): wsVar(x) = ws.Name: x = x + 1 End If Next ws Range("A1").Resize(x) = Application.Transpose(wsVar) End Sub
How do you mean "dynamically", does this mean you want the list to change when sheets are hidden/ made visible?
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim ws As Worksheet, wsVar() As Variant, x As Long
If Sh.Name <> "Sheet1" Then Exit Sub
For Each ws In Sheets
If Not ws.Visible Then
ReDim Preserve wsVar(x): wsVar(x) = ws.Name: x = x + 1
End If
Next ws
With Sheet1.Range("A1")
.Resize(Sheets.Count).ClearContents
If x > 0 Then .Resize(x) = Application.Transpose(wsVar)
End With
End Sub
Not necessarily, as long as x is defined as long the default value of x will be zero, as the array can start at zero, there is no need to increment x before use.@Georgiboy shouldn’t this be before your Redim statement.
x = x + 1
Thanks a lot, it worked for me.If you are hiding/ unhiding the sheets manually then you can try the below in the 'ThisWorkbook' module:
VBA Code:Private Sub Workbook_SheetActivate(ByVal Sh As Object) Dim ws As Worksheet, wsVar() As Variant, x As Long If Sh.Name <> "Sheet1" Then Exit Sub For Each ws In Sheets If Not ws.Visible Then ReDim Preserve wsVar(x): wsVar(x) = ws.Name: x = x + 1 End If Next ws With Sheet1.Range("A1") .Resize(Sheets.Count).ClearContents If x > 0 Then .Resize(x) = Application.Transpose(wsVar) End With End Sub
Where "Sheet1" in the code refers to the sheet where the sheet names will be listed.