Developing an Array Function that Gets all the Worksheets Name property with Conditions

Luthius

Active Member
Joined
Apr 5, 2011
Messages
324
Guys
I would like to develop a function that can retrieve/get the Worksheet names from the current workbook as an array function.
The criteria is only Visible WorkSheets and the name <> "Setup".

I tried the code below but it is NOT working properly
Can you guys help me



Code:
[COLOR=#0000ff]Public Function[/COLOR] GetSpreadSheetList() [COLOR=#0000ff]As String[/COLOR]()

[COLOR=#0000ff]    Dim[/COLOR] ws [COLOR=#0000ff]As [/COLOR]Worksheet
    [COLOR=#0000ff]Dim[/COLOR] wb [COLOR=#0000ff]As[/COLOR] Workbook
    [COLOR=#0000ff]Dim[/COLOR] item [COLOR=#0000ff]As[/COLOR] Variant
    [COLOR=#0000ff]Dim[/COLOR] arr() [COLOR=#0000ff]As[/COLOR] String
    
    [COLOR=#0000ff]Set[/COLOR] wb = Application.ThisWorkbook
    
    [COLOR=#0000ff]For Each[/COLOR] ws [COLOR=#0000ff]In[/COLOR] wb.Worksheets
        [COLOR=#0000ff]If [/COLOR]ws.Name <> "Setup" [COLOR=#0000ff]And[/COLOR] ws.Visible [COLOR=#0000ff]Then[/COLOR]
            [COLOR=#0000ff]ReDim Preserve [/COLOR]arr([COLOR=#0000ff]LBound[/COLOR](arr) [COLOR=#0000ff]To[/COLOR] [COLOR=#0000ff]UBound[/COLOR](arr))
            arr(item) = ws.Name
[COLOR=#0000ff]        End If[/COLOR]
[COLOR=#0000ff]    Next[/COLOR]
    
    [COLOR=#0000ff]Set [/COLOR]wb = [COLOR=#0000ff]Nothing[/COLOR]
[COLOR=#0000ff]End Function[/COLOR]
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about:

Code:
Public Function GetSpreadSheetList() As Variant

    Dim ws As Worksheet
    Dim wb As Workbook
    Dim arr() As String
    Dim ctr As Long
    
    Set wb = Application.ThisWorkbook

    For Each ws In wb.Worksheets
        If ws.Name <> "Setup" And ws.Visible Then
            ctr = ctr + 1
            ReDim Preserve arr(1 To ctr)
            arr(ctr) = ws.Name
        End If
    Next

    Set wb = Nothing
    GetSpreadSheetList = WorksheetFunction.Transpose(arr)
    
End Function
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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