Worksheets to wsArray() ?

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
I have a named range (dynamic) at :

wb1.Worksheets("INDEX").Range("ExcludedTabs")

I open up another workbook (wb2). Now I would like to build an array of all worksheets in wb2, provided the worksheets is not matched/found in the range "ExcludedTabs"

What is the best way to do this?

Also I would like to omit the need to reference worksheet "INDEX" as this may change

Many thanks :)
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Maybe (completely untested):

Code:
Sub Test()
    Dim avsWks      As Variant
    Dim wkb         As Workbook
    Dim wb1         As Workbook

    Set wkb = Workbooks("Bob.xls")
    Set wb1 = Workbooks("Joe.xls")
    avsWks = GetDemWks(wkb, wb1.Worksheets("INDEX").Range("ExcludedTabs"))

    If IsArray(avsWks) Then
        MsgBox "Got " & UBound(avsWks) & " worksheets"
    Else
        MsgBox "Nada"
    End If
End Sub

Function GetDemWks(wkb As Workbook, r As Range) As Variant
    Dim asWks()     As String
    Dim sWks        As String
    Dim wks         As Worksheet
    Dim iWks        As Long
    Dim nWks        As Long

    ReDim asWks(1 To wkb.Worksheets.Count)

    With WorksheetFunction
        For iWks = 1 To wkb.Worksheets.Count
            sWks = wkb.Worksheet(iWks).Name
            If .CountIf(r, sWks) = 0 Then
                nWks = nWks + 1
                asWks(nWks) = sWks
            End If
        Next iWks
    End With

    If nWks Then
        ReDim Preserve asWks(1 To nWks)
        GetDemWks = asWks
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,207,255
Messages
6,077,313
Members
446,278
Latest member
hoangquan2310

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