Thomas
Thomas

Reputation: 867

Getting sheet indice from Sheet Name in VBA

I need to pull a sheet's position in a workbook knowing only it's name -- so for instance:

if we have a sheet, say
Workbook.Sheets("Sheet2")

how would I find the corresponding integer so that I could refer to it as: say i is an integer
Workbook.Sheets(i)

I need to be able to do this reverse indicing so I can refer to sheets next to the sheet I'm referencing.

Thank you for your help.

Upvotes: 2

Views: 7300

Answers (2)

Daniel
Daniel

Reputation: 13142

Workbook.Sheets("sheet name").Index

Edit: brettdj's answer inspired me, so I wrote this function which could probably be cleaned up infact thinking about it, if I were actually going to use and support this I would probably make a find sheet function instead of what the sub does if you say true for the 4th parameter:

Function adjacentsheet(Optional ws As Worksheet, Optional wsName As String, Optional nextSheet As Boolean = True, Optional search As Boolean = False) As Worksheet
'Expects worksheet or worksheet.name if blank, uses activesheet.
'Third parameter indicates if the next sheet or previous sheet is wanted, default is next = true
'Indicates adjacent sheet based on worksheet provided.
'If worksheet is not provided, uses worksheet.name.
'If no worksheet matches corresponding name, checks other workbooks if 4th parameter is true
'If no worksheet can be found, alerts the user.
'Returns found worksheet based upon criteria.


If (ws Is Nothing) Then
    If wsName = "" Then
        Set adjacentsheet = adjacentsheet(ActiveSheet, , nextSheet)
    Else
        'Check all workbooks for the wsName, starting with activeWorkbook
        On Error Resume Next
        Set ws = Sheets(wsName)
        On Error GoTo 0
        If (ws Is Nothing) Then
            If search = True Then
                If Workbooks.Count = 1 Then
                    GoTo notFound
                Else
                    Dim wb As Workbook
                    For Each wb In Application.Workbooks
                        On Error Resume Next
                        Set ws = wb.Sheets(wsName)
                        On Error GoTo 0
                        If Not (ws Is Nothing) Then
                            Set adjacentsheet = adjacentsheet(ws, , nextSheet)
                            Exit For
                        End If
                    Next
                    If (ws Is Nothing) Then GoTo notFound
                End If
            Else
                GoTo notFound
            End If
        Else
            Set adjacentsheet = adjacentsheet(ws, , nextSheet, search)
        End If
    End If
Else
    With ws.Parent
        If nextSheet Then
            If ws.Index = .Sheets.Count Then
                Set adjacentsheet = .Sheets(1)
            Else
                Set adjacentsheet = .Sheets(ws.Index + 1)
            End If
        Else
            If ws.Index = 1 Then
                Set adjacentsheet = .Sheets(.Sheets.Count)
            Else
                Set adjacentsheet = .Sheets(ws.Index - 1)
            End If
        End If
    End With
End If
Exit Function
notFound:
MsgBox "Worksheet name could not be found!", vbCritical, "Invalid worksheet name."

End Function

Here are some usage examples: 'Usage Examples

Dim nextws As Worksheet
'returns sheet before the active sheet
Set nextws = adjacentsheet(, , False)
'returns sheet after the active sehet
Set nextws = adjacentsheet()
'returns sheet after sheet named "Test" in current workbook
Set nextws = adjacentsheet(, "Test")
'returns sheet after sheet named "Test" in any open workbook checking current workbook first
Set nextws = adjacentsheet(, "Test", , True)

Upvotes: 4

brettdj
brettdj

Reputation: 55702

If you want to refer to the Next or Previous sheet then you can do this without incrementing the starting sheet position

Sub Test()
If Sheets.Count > ActiveSheet.Index Then
Debug.Print "next method: " & ActiveSheet.Next.Name
Debug.Print "index method: " & Sheets(ActiveSheet.Index + 1).Name
Else
Debug.Print "Active Sheet is the last sheet"
End If
End Sub

Upvotes: 1

Related Questions