我收到錯誤下標超出範圍(錯誤9)在vba中。我無法找到問題,我到處搜索它。我的代碼要求用戶輸入工作簿名稱,然後在用戶選擇工作表後請求選擇工作表。它將在兩張紙之間複製和粘貼。是否有人可以幫助我,這裏是我的代碼,如果你運行它,那麼你會看到錯誤感謝下標超出範圍(錯誤9)在vba
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
Sub Button1_Click()
Dim shname As String
Dim wb As String
wb = Application.GetOpenFilename
If wb <> "False" Then Workbooks.Open wb
Do Until WorksheetExists(shname)
shname = InputBox("Enter sheet name")
If Not WorksheetExists(shname) Then
MsgBox shname & " doesn't exist!", vbExclamation
Else
WSName = shname
Sheets(shname).Select
ActiveWorkbook.Worksheets("Sheet1").Cells(1, 1) = Workbooks(Dir(wb)).Worksheets(shname).Cells(1, 1)
End If
Loop
End Sub
你有「Option Explicit」嗎? (確實如此。) – dcromley
工作簿'book11'從哪裏來?它是否在excel的相同實例中打開? – RowanC
book11這是活動工作簿,並試圖打開另一個工作簿,並選擇一個工作表,然後複製和粘貼兩張紙 – user3793997