2012-08-27 4 views
2

에 시트 이름에서 시트 indice을 얻기 만 이름입니다 옆에 내가
Workbook.Sheets(i)내가 통합 문서가 아는 시트의 위치를 ​​당겨야 VBA

가 나는 이것이 내가 시트를 참조 할 수 있습니다 indicing 반전 할 수 있어야 정수라고 : 나는 그것을 참조 할 수 있도록 해당 정수를 찾아 낼 것입니다 나는 참조하고있다.

도움 주셔서 감사합니다.

답변

4
Workbook.Sheets("sheet name").Index 

편집 : brettdj의 대답은 나에게 영감을, 그래서 실제로 나는 아마 찾기 시트 기능을 만들 것 사용하고 지원하려고한다면 나는 아마 그것에 대해 가리키고 생각을 정리 할 수있는이 기능을 썼다 여기

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 

가 일부 사용 예는 다음과 같습니다 : 대신에 당신이 4 매개 변수에 true를 말한다면, 서브가하는 일의 '사용 예

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) 
,
1

당신이 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 
+0

감사를 증가하지 않고이 작업을 수행 할 수 있습니다. 내 대답에 다소 어리석은 편집을하도록 영감을 주었다. –

관련 문제