VBA list sheetnames but exclude hidden sheets

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
125
Hi,

Looking for some assistance. I have the following code that lists all worksheets from an open workbook.

Code:
Sub ListWS()

Dim FolderPath1 As String


Dim data1 As Worksheet


Set data1 = Worksheets("DATA")


FolderPath1 = data1.Cells(5, 3)
FileName1 = data1.Cells(4, 3)
    


FileName2 = Dir(FolderPath1 & "*.xlsx")


With Sheets("WORKING").Activate
End With


    Set wbk = Workbooks(FileName2)
    
    Set wbk = ActiveWorkbook


            For i = 3 To Workbooks(FileName2).Sheets.Count
            Cells(i, 2) = Workbooks(FileName2).Sheets(i).Name
            Next i


End Sub

This works fine however I would like to ignore any hidden worksheet which this currently isn't doing. I had tried to add

Code:
If ws.Visible = True Then
For i = 3 To Workbooks(FileName2).Sheets.Count

But I get a Run-time error. Any idea where I'm going wrong?

Thanks,
EMcK
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this

Code:
Sub ListWS()


    Dim FolderPath1 As String
    Dim data1 As Worksheet
    
    Set data1 = Worksheets("DATA")
    
    FolderPath1 = data1.Cells(5, 3)
    FileName1 = data1.Cells(4, 3)
    FileName2 = Dir(FolderPath1 & "*.xlsx")
    
    With Sheets("WORKING").Activate
    End With
    
    Set wbk = Workbooks(FileName2)
    Set wbk = ActiveWorkbook


[COLOR=#0000ff]    Dim n As Long[/COLOR]
[COLOR=#0000ff]    n = 3[/COLOR]
    For i = 3 To Workbooks(FileName2).Sheets.Count
[COLOR=#0000ff]        If Workbooks(FileName2).Sheets(i).Visible = True Then[/COLOR]
            Cells([COLOR=#0000ff]n[/COLOR], 2) = Workbooks(FileName2).Sheets(i).Name
[COLOR=#0000ff]            n = n + 1[/COLOR]
        End If
    Next i
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,520
Messages
6,114,101
Members
448,548
Latest member
harryls

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