[英]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. 谢谢您的帮助。
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: 编辑: brettdj的回答启发了我,所以我写了这个函数,实际上可以考虑一下它,如果我真的要使用和支持它,我可能会创建一个find sheet函数,而不是sub的功能,如果你说对于第四个参数为true:
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)
If you want to refer to the Next
or Previous
sheet then you can do this without incrementing the starting sheet position 如果要参考Next
或Previous
一张纸,则可以在不增加起始纸位置的情况下进行操作
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.