[英]Excel VBA Check if File is open function
This seems to be an easy function and solution should be straight forward, but I can't find the problem.这似乎是一个简单的 function 和解决方案应该是直截了当的,但我找不到问题。
I have a function that gets called in a sub, it checks if the file is open, if not, to open it.我有一个在子程序中调用的 function,它检查文件是否打开,如果没有,则打开它。 The function runs perfectly but when it returns to the main sub that's calling it, the variable (True or False) loses its value and I get an error 9: subscript out of range on the line
Set wb = Workbooks(MasterFileF)
in the main sub. 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
Where am I going wrong that the Function variables' values get lost when it returns to the main sub?当 Function 变量的值返回到主子时会丢失,我哪里出错了?
I'd turn a litle bit your code我会稍微修改一下你的代码
have the Wb Open() f
unction return the open workbook, if found, via its arguments让 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
and then in your main code simply go:然后在您的主代码中只需 go:
MasterFile = Dir(path & "\*Master data*.xls*")
If Not wbOpen(MasterFile, wb) Then Set wb = Workbooks.Open(path & "\" & MasterFile)
Edit编辑
to add an enhanced version to handle workbook with same names but different paths添加增强版本以处理具有相同名称但不同路径的工作簿
in this case you have to check both the file name and the path, but in different steps在这种情况下,您必须同时检查文件名和路径,但步骤不同
so WbOpen()
function becomes:所以
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
and the relevant part of your main code would change to:并且您的主要代码的相关部分将更改为:
MasterFile = Dir(path & "\*.xls*")
If Not wbOpen(MasterFile, path, wb) Then Set wb = Workbooks.Open(path & "\" & MasterFile)
I think the problem lies in your wbOpen
function.我认为问题在于您的
wbOpen
function。 You're setting that workbook object locally and not returning a value for the Boolean
.您正在本地设置该工作簿 object 并且不返回
Boolean
的值。 See below:见下文:
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.