1
下面的代碼工作不正常。嘗試運行宏時遇到錯誤400。你可以稍微回顧一下這段代碼嗎?我不確定問題是否與我引用的函數變量不同。Excel VBA檢查工作簿是否打開,如果不是,打開它
Sub AutoFinal()
Dim final_wb As Workbook, shop_stat_wb As Workbook
Dim book2 As String
book2 = "Workbook_I_need.xlsx"
Dim book2path As String
book2path = ThisWorkbook.Path & "\" & book2
Set final_wb = ThisWorkbook
If IsOpen(book2) = False Then Workbooks.Open (book2path)
Set shop_stat_wb = Workbooks(book2)
End Sub
Function IsOpen(strWkbNm As String) As Boolean
On Error Resume Next
Dim wBook As Workbook
Set wBook = Workbooks(strWkbNm)
If wBook Is Nothing Then 'Not open
IsOpen = False
Set wBook = Nothing
On Error GoTo 0
Else
IsOpen = True
Set wBook = Nothing
On Error GoTo 0
End If
End Function
它錯誤在哪一行? – Zerk
我想這個錯誤出現在Workbooks.Open的行上。在那裏設置一個斷點,並檢查Path是否正確,並且指向一個現有的文件。 – z32a7ul
@ z32a7ul是的,您對錯誤行是正確的。我檢查了一條路徑是正確的。設置斷點不是一個問題,但我可以用這個來實現嗎? – RafMil