[英]Excel VBA Check if File is open function
這似乎是一個簡單的 function 和解決方案應該是直截了當的,但我找不到問題。
我有一個在子程序中調用的 function,它檢查文件是否打開,如果沒有,則打開它。 function 運行完美,但是當它返回到調用它的主子時,變量(True 或 False)失去了它的值,我收到錯誤 9: subscript out of range on the line Set wb = Workbooks(MasterFileF)
in the main子。
Function wbOpen(wbName As String) As Boolean
Dim wbO As Workbook
On Error Resume Next
Set wbO = Workbooks(wbName)
wbOpen = Not wbO Is Nothing
Set wbO = Nothing
End Function
Sub Macro5()
Dim wb As Workbook
Dim path As String
Dim MasterFile As String
Dim MasterFileF As String
Application.ScreenUpdating = False
'Get folder path
path = GetFolder()
If path = "" Then
MsgBox "No folder selected. Please start macro again and select a folder"
Exit Sub
Else
End If
MasterFile = Dir(path & "\*Master data*.xls*")
MasterFileF = path & "\" & MasterFile
'Check if workbook open if not open it
If wbOpen(MasterFile) = True Then
Set wb = Workbooks(MasterFileF)
Else
Set wb = Workbooks.Open(MasterFileF)
End If
當 Function 變量的值返回到主子時會丟失,我哪里出錯了?
我會稍微修改一下你的代碼
讓 Wb Open() f
返回打開的工作簿(如果找到),通過其 arguments
Function wbOpen(wbName As String, wbO As Workbook) As Boolean
On Error Resume Next
Set wbO = Workbooks(wbName)
wbOpen = Not wbO Is Nothing
End Function
然后在您的主代碼中只需 go:
MasterFile = Dir(path & "\*Master data*.xls*")
If Not wbOpen(MasterFile, wb) Then Set wb = Workbooks.Open(path & "\" & MasterFile)
編輯
添加增強版本以處理具有相同名稱但不同路徑的工作簿
在這種情況下,您必須同時檢查文件名和路徑,但步驟不同
所以WbOpen()
function 變為:
Function wbOpen(wbName As String, wbPath As String, wbO As Workbook) As Boolean
On Error Resume Next
Set wbO = Workbooks(wbName)
On Error GoTo 0 ' restore error handling back
If Not wbO Is Nothing Then ' in current excel session there already is an open workbook with same name (path excluded) as the searched one
If wbO.path = wbPath Then ' the already open workbook has the same path as the searched one -> we got it!
wbOpen = True
Else ' the already open workbook has a different path from the searched one -> we must investigate ...
If MsgBox("A workbook named after:" _
& vbCrLf & vbCrLf & vbTab & wbName _
& vbCrLf & vbCrLf & " is already open but its path is different from:" _
& vbCrLf & vbCrLf & vbTab & wbPath _
& vbCrLf & vbCrLf & "If you want to open the new found one, the already open one will be closed" _
& vbCrLf & vbCrLf & vbCrLf & "Do you want to open the new found one?", vbQuestion + vbYesNo) = vbYes Then
wbO.Close True ' close the currently opened workbook with same name but different path from searched one
' the opening of the new one will be made in the main sub, after this function returning 'False'
Else
wbOpen = True ' you chose not to open the searched one and stay with the currently open one -> return 'True' to say you are done
End If
End If
End If
End Function
並且您的主要代碼的相關部分將更改為:
MasterFile = Dir(path & "\*.xls*")
If Not wbOpen(MasterFile, path, wb) Then Set wb = Workbooks.Open(path & "\" & MasterFile)
我認為問題在於您的wbOpen
function。 您正在本地設置該工作簿 object 並且不返回Boolean
的值。 見下文:
Function wbOpen(ByVal wbName As String) As Boolean
Dim wbO As Workbook
For Each wbO In Application.Workbooks
If InStr(1, wbO.Name, wbName) Then
wbOpen = True
Exit Function
End If
Next wbO
wbOpen = False
End Function
Sub Macro5()
Dim wb As Workbook
Dim path As String
Dim MasterFile As String
Dim MasterFileF As String
Application.ScreenUpdating = False
'Get folder path
path = GetFolder()
If path = "" Then
MsgBox "No folder selected. Please start macro again and select a folder"
Application.ScreenUpdating = True
Exit Sub
End If
MasterFile = Dir(path & "\*Master data*.xls*")
MasterFileF = path & "\" & MasterFile
'Check if workbook open if not open it
If wbOpen(MasterFile) = True Then
Set wb = Workbooks(MasterFileF)
Else
Set wb = Workbooks.Open(MasterFileF)
End If
Application.ScreenUpdating = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.