【问题标题】:Getting sheet indice from Sheet Name in VBA从 VBA 中的工作表名称获取工作表索引
【发布时间】:2012-08-22 04:39:50
【问题描述】:

我需要在工作簿中拉出工作表的位置,只知道它的名称——例如:

如果我们有工作表,请说
Workbook.Sheets("Sheet2")

我将如何找到相应的整数,以便我可以将其称为: 说 i 是一个整数
Workbook.Sheets(i)

我需要能够执行此反向指示,以便我可以参考我正在参考的工作表旁边的工作表。

感谢您的帮助。

【问题讨论】:

    标签: excel vba


    【解决方案1】:
    Workbook.Sheets("sheet name").Index
    

    编辑: brettdj 的回答启发了我,所以我写了这个函数,实际上考虑到它可能会被清理,如果我真的要使用并支持它,我可能会制作一个查找表如果您对第 4 个参数说 true,则函数而不是 sub 的作用:

    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
    

    以下是一些使用示例: '使用示例

    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)
    

    【讨论】:

      【解决方案2】:

      如果您想引用NextPrevious 表,那么您可以在不增加起始表位置的情况下执行此操作

      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
      

      【讨论】:

      • 感谢您的示例。您启发了我对我的答案进行了一些荒谬的编辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多