 Listing Worksheets In VBA
    Listing Worksheets In VBA
    
    This page describes VBA code to get a list of worksheets.
     
    
    
    
    
    
    
    
    
    
    You might find it useful to get a list of existing worksheet names. For example, you might want to allow the
    user to select a specific sheet for processing. This page describes a VBA Function that will return
    an array of worksheet names to a specific range on the worksheet. It also contains a function
    to be called by other VBA code.
    
    
    The main function is called SheetNames and has the following 
    declaration.
    
    
    Function SheetNames(Optional R As Range, _
        Optional FromIndex As Long = -1, _
        Optional ToIndex As Long = -1, _
        Optional VisibleOnly As Boolean = False) As String()
    
    
    
    The code for SheetNames is below.
    
Function SheetNames(Optional R As Range, _
    Optional FromIndex As Long = -1, _
    Optional ToIndex As Long = -1, _
    Optional VisibleOnly As Boolean) As String()
    Dim WS As Worksheet
    Dim Res() As String
    Dim CalledBy As Range
    Dim N As Long
    Dim UpperBound As Long
    Dim WB As Workbook
    Dim FromNdx As Long
    Dim ToNdx As Long
    Dim Cl As Long
    Dim Rw As Long
    Dim SheetNdx As Long
    Dim DoThisSheet As Boolean
    
    Set CalledBy = Application.Caller
    If R Is Nothing Then
        Set WB = CalledBy.Parent.Parent
    Else
        Set WB = R.Worksheet.Parent
    End If
    
    If FromIndex < 0 Then
        FromNdx = 1
    Else
        FromNdx = FromIndex
    End If
    If ToIndex < 0 Then
        ToNdx = WB.Worksheets.Count
    Else
        ToNdx = ToIndex
    End If
    
    If CalledBy.Rows.Count > 1 Then
        UpperBound = Application.WorksheetFunction.Max(CalledBy.Rows.Count, _
                ThisWorkbook.Worksheets.Count)
        ReDim Res(1 To UpperBound, 1 To 1)
    Else
        UpperBound = Application.WorksheetFunction.Max(CalledBy.Columns.Count, _
                ThisWorkbook.Worksheets.Count)
        ReDim Res(1 To 1, 1 To UpperBound)
    End If
    For Each WS In WB.Worksheets
        If WS.Index <= ToNdx And WS.Index >= FromNdx Then
            DoThisSheet = False
            If VisibleOnly = True Then
                 If WS.Visible = xlSheetVisible Then
                    DoThisSheet = True
                Else
                    DoThisSheet = False
                End If
            Else
                DoThisSheet = True
            End If
            
            If DoThisSheet = True Then
                N = N + 1
                If CalledBy.Rows.Count > 1 Then
                    Res(N, 1) = WS.Name
                Else
                    Res(1, N) = WS.Name
                End If
            End If
        End If
    Next WS
    
    SheetNames = Res
    End Function
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<< END CODE
     
 
     
 
To return a single dimensional array of worksheet names, use the following code. The parameters here
have the same meaning as in SheetNames.
 
 
Function SheetNamesArray(Optional R As Range, _
    Optional ByVal FromIndex As Long = -1, _
    Optional ByVal ToIndex As Long = -1, _
    Optional VisibleOnly As Boolean) As String()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SheetNamesArray
' This function returns a single-dimensional array names
' according to the FromIndex, ToIndex, and VisibleOnly
' properties.
'
' You can restrict which worksheets are returned by using the FromIndex
' and ToIndex paramaters. FromIndex is the inclusive lower bound of the
' worksheets to return. Worksheets whose Index properties are less than
' FromIndex are not returned in the array. If FromIndex is < 0 or omitted,
' the enumeration begins with Worksheets(1). If present, the enumeration
' begins with Worksheet(FromIndex). ToIndex can be used to control the
' upper region of the array. If ToIndex is omitted or < 0, the numeration
' ends at the last sheet. If ToIndex is present, the enumeration ends at
' Worksheets(ToIndex). FromIndex and ToIndex are inclusive.
'
' If both ToIndex and FromIndex are omitted or both
' are < 0, all worksheets are returned. If ToIndex is less than FromIndex,
' no worksheets are returned. By default,
'
' By default, all worksheet names are returned, subject to the constraints
' of FromIndex and ToIndex. You can get a list of only visible worksheets
' by setting the VisibleOnly parameter to True. If True, only visible
' worksheets are returned in the array. If False or omitted, all worksheets
' are returned.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
Dim Res() As String
Dim WB As Workbook
Dim WS As Worksheet
Dim N As Long
Dim DoThisWS As Boolean
If Not R Is Nothing Then
    Set WB = R.Worksheet.Parent
Else
    Set WB = ThisWorkbook
End If
ReDim Res(1 To WB.Worksheets.Count)
For Each WS In WB.Worksheets
    DoThisWS = True
    If FromIndex > 0 Then
        If FromIndex < WS.Index Then
            DoThisWS = False
        End If
    End If
    If ToIndex > 0 Then
        If ToIndex > WS.Index Then
            DoThisWS = False
        End If
    End If
    If VisibleOnly = True Then
        If WS.Visible <> xlSheetVisible Then
            DoThisWS = False
        End If
    End If
    If DoThisWS = True Then
        N = N + 1
        Res(N) = WS.Name
    End If
    
Next WS
ReDim Preserve Res(1 To N)
SheetNamesArray = Res
End Function
    
    
    
        
     
    
        
            |  | This page last updated: 29-Sept-2012. |