Function SheetNames(Ref As Range, _ Optional FromIndex As Long = -1, _ Optional ToIndex As Long = -1, Optional VisibleOnly As Boolean) As String() ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' SheetNames ' By Chip Pearson, 28-Sept-2012 chip@cpearson.com , ' www.cpearson.com/Excel/WorksheetNames.aspx ' ' This function returns a String() array, each element of which is name ' of a worksheet. The function will return the names of the worksheets ' contained in the workbook containing the cell Ref. It does not matter what ' cell is referenced, so long as it is in the workbook whose sheet names are to ' be listed. ' ' The function needs to be array-entered (http://www.cpearson.com/Excel/ArrayFormulas.aspx) ' in a range that is either one row high spanning several columns or a single column ' spanning several rows. The size and orientation of the result array ' are matched to the range from which the function was called. If the range ' has more than one column and more than one range, the worksheets are listed ' in the first column and repeated in subsequent columns. It is not recommended ' that you enter the formula in a range that has both more than one row and ' more than one column. ' ' To use the function, select the range into which the results are to be ' returned, type =SheetNames(A1,other_params) and press CTRL SHIFT ENTER instead 'of just ENTER. See http://www.cpearson/com/Excel/ArrayFormulas.aspx for ' much more information about array formulas. The formula ' must be array entered with CTRL SHIFT ENTER in order to work properly. It ' will not work correctly if you do not enter it with CTRL SHIFT ENTER. ' ' If the range contains fewer cells that there are worksheets in the ' workbook, the final (right-most) worksheet names are not returned. If the range ' into which the formula is entered contains more cells that there are ' worksheets, the excess elements of the array is filled with vbNullStrings, ' which will suppress the usual #N/A errors that would otherwise arise. ' ' 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). The ToIndex parameter 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 FromIndex is ' greater than the number of worksheets, no worksheets are in the array. If ' ToIndex is greater than the number of worksheets, the enumeration ends at ' the last worksheet. ' ' If both ToIndex and FromIndex are omitted or both ' are <= 0, all worksheets are returned to the array. If ToIndex is less ' than FromIndex, no worksheets are returned. ' ' By default, all worksheet names are returned, subject to the constraints ' of the FromIndex and ToIndex parameters. 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. ' ' This function uses Application.Caller to get the size and orientation ' of the output range. Therefore, it cannot be used by other VBA code. ' See the procedure SheetNamesArray in this module for code that creates ' a filtered list of workshet nams in a 1-dimensional array, suitable for ' being called by other VBA procedures. The parameters and their meanings ' to SheetNamesArray are the same as for this procedure. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''</span> 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
Set WB = Ref.Worksheet.Parent 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
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<< BEGIN CODE
Function SheetNamesArray(WB As Workbook, _ 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 of worksheet names ' according to the WB, FromIndex, ToIndex, and VisibleOnly ' parameters. Sheets in the workbook referenced by the WB ' parameter are listed in the array. This parameter should be ' to a cell on a worksheeet in the workbook whose sheet names you ' want to return as the result. It does not matter where Ref ' points to. It is only used to get a reference to the workbook. ' ' 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 WS As Worksheet Dim N As Long Dim DoThisWS As Boolean 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 '<<<<<<<<<<<<<<<<<<<<<<<<<<<<< END CODE