【问题标题】:Excel VBA Check if File is open functionExcel VBA检查文件是否打开功能
【发布时间】:2020-04-06 12:16:10
【问题描述】:

这似乎是一个简单的功能,解决方案应该是直截了当的,但我找不到问题。

我有一个在子程序中调用的函数,它检查文件是否打开,如果没有打开它。 该函数运行完美,但是当它返回到调用它的主 sub 时,变量(True 或 False)失去了它的值,我收到错误 9: subscript out of rangeSet wb = Workbooks(MasterFileF) 行在主子中。

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

函数变量的值在返回主子时丢失,我哪里出错了?

【问题讨论】:

  • 您是否尝试过使用F8 单步执行您的代码?
  • 您也在使用On Error Resume Next,但请重置它。使用On Error Goto 0 来做到这一点。另外,我不会使用这种方法,我会遍历Application.Workbooks 集合中的Workbooks 来查找工作簿。
  • 这就是我当前运行代码以查看问题所在的方式。我使用监视窗口查看问题出在哪里。一切都很好,直到它返回到主子,然后 wbOpen 应该是 TrueFalse空了
  • 好的,我想我知道出了什么问题,现在正忙于解决方案。

标签: excel vba function variables boolean


【解决方案1】:

我会稍微修改一下你的代码:

WbOpen() 函数通过其参数返回打开的工作簿(如果找到)

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

然后在你的主代码中简单地去:

MasterFile = Dir(path & "\*Master data*.xls*")

If Not wbOpen(MasterFile, wb) Then Set wb = Workbooks.Open(path & "\" & MasterFile)

编辑

添加一个增强版本来处理同名但不同路径的工作簿

在这种情况下,您必须同时检查文件名和路径,但步骤不同

所以WbOpen()函数变成:

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)

【讨论】:

  • @SimoneFick,查看编辑后的答案以处理搜索到的同名但路径不同的工作簿,然后打开那些
【解决方案2】:

我认为问题在于您的wbOpen 函数。您在本地设置该工作簿对象,而不是返回 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

【讨论】:

  • 我将代码更改为上面的代码,但在相同的代码上我仍然遇到同样的问题,并且仍然失去布尔值,不幸的是。
  • 如果您在Set wb = Workbooks(MasterFileF) 线上收到error 9: subscript out of range... a) 错误时MasterFileF 的值是多少。此外,如果它正在读取该行,那么您的函数将返回 true。所以错误会出现在MasterFileFString...?您尝试使用不正确的地址/名称/打开工作簿不存在。
  • MasterFileF 给了我预期的文件名和路径的字符串。我在错误行中将MasterfileF 更改为MasterFile (这只是文件名),这似乎有效......我也需要使用路径文件进行设置。有什么理由会发生这种情况?
  • @SimoneFick " ...我将 MasterfileF 更改为 MasterFile...",这正是我的答案...
  • @HTH 抱歉,我的网页没有更新您编辑的答案。我不得不刷新我的页面。成功了,谢谢!我刚刚用MasterfileF替换了path & "\" & MasterFile形式你的答案
猜你喜欢
  • 2015-08-02
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 2019-06-05
相关资源
最近更新 更多