【问题标题】:How to exit multiple macros if file isnt located?如果找不到文件,如何退出多个宏?
【发布时间】:2021-05-19 23:24:49
【问题描述】:

Call_Process 调用多个宏,每个宏都依赖于前一个宏。第一个宏获取文件位置,然后打开该文件夹中的文件,最后一个关闭并保存更改。问题是如果打开文件不存在,以下宏将出错。如果找不到文件,如何取消整个 Call_Process?

Sub Call_Process ()
Call File_Location
Call Open_File
Call Exit_WB
End Sub

**Sub File_Location ()**
location_db = "C:\Users\Documents\New folder\*.xl??"
End Sub ()

**Sub Open_File ()**
dim wb as Workbook
dim ws as Worksheet

if dir(location_db) = "" <- as you can see if the file doesnt exists, it exits out of Open_File sub but not the Exit_WB when im running the Call_Process
end sub
end if

wb_filename = wb.name
sheet_name = ws.name
End Sub


**Sub Exit_WB**
Workbooks(location_db).close savechanges=TRUE
End Sub

【问题讨论】:

  • Try on error goto: on your Call process?
  • 试试“结束”?
  • File_Location 应该是一个属性或一个函数,如果不只是一个普通的 Const 声明。 location_db 没有理由是全局的,而是让 Open_File 将其作为参数接收。如果文件不存在,则在调用范围(即Call_Process)中引发运行时错误并处理它,使用On Error 语句将执行重定向到提前退出(@ 987654328@ 应该足够了)发生错误时。 End 语句会破坏所有全局变量。使Open_File 成为一个返回Workbook 引用的函数,这样Exit_WB 就不需要去找它了。

标签: excel vba


【解决方案1】:

说实话,您的应用程序需要更多的结构。我认为调用更新全局变量的 SubProcedures 不是一个好习惯(除非有特定的需要)。看看我下面的例子。 Call_Process 首先向函数询问文件路径。如果它没有得到一个,子例程处理它自己的错误消息。一旦路径可用,它就会调用一个函数来打开一个工作簿并留下对所述工作簿的引用。同样,如果它不起作用,子可以自己处理。

如果这有帮助,请告诉我。

Sub Call_Process()
    Dim filePath As String
    Dim wb As Workbook
    
    filePath = GetDatabaseLocation()
    
    If filePath = vbNullString Then
        MsgBox "No File"
        Exit Sub
    End If
    
    
    Set wb = GetDatabaseWorkbook(filePath)
    
    If wb Is Nothing Then
        MsgBox "Sorry, couldn't open this file."
        Exit Sub
    End If
    
    'Process whatever
    
    wb.Close SaveChanges:=True
End Sub


Private Function GetDatabaseLocation() As String
    Dim loc As String
    Dim fullPath As String
    
    loc = "c:\TempPath\*.xl??"
    
    'If the file doesn't exist, it will return an empty string
    'Otherwise, the file location. NOTE: DIR() returns only the file
    'name, so we will append the path below
    fullPath = Dir(loc)
    
    If fullPath <> vbNullString Then
        fullPath = "c:\TempPath\" & fullPath
    End If
    
    GetDatabaseLocation = fullPath
End Function

Private Function GetDatabaseWorkbook(sFilename As String) As Workbook
    Dim wb As Workbook
    
    'If there is an error or something, will return 'Nothing'
    On Error Resume Next
    Set wb = Workbooks.Open(sFilename)
    On Error GoTo 0
    
    Set GetDatabaseWorkbook = wb
End Function

【讨论】:

    【解决方案2】:

    您也可以使用 on error 进行编码,如果错误触发器应该可以工作:

    Sub Call_Process ()
    
    On Error GoTo ErrorHandler:
    Call File_Location
    Call Open_File
    Call Exit_WB
    
    Exit Sub
    ErrorHandler:
    End Sub
    

    【讨论】:

    • 您应该在打开文件之前检查文件是否存在。不要使用错误处理。
    【解决方案3】:

    当您说取消时...您可以在代码中使用“END”。这将阻止整个代码运行。注意,它实际上不会告诉您它已停止,因此您可能希望将评论添加为消息框或即时窗口。代码看起来像这样:


    Sub Call_Process()
    Call File_Location
    Call Open_File
    Call Exit_WB
    End Sub
    
    Sub File_Location()
    
    location_db = "C:\Users\Documents\New folder\*.xl??"
    End Sub
    
    Sub Open_File()
    Dim wb As Workbook
    Dim ws As Worksheet
    
    If Dir(location_db) = "" Then
        MsgBox "no file location"
        End
    End If
    
    wb_filename = wb.Name
    sheet_name = ws.Name
    End Sub
    
    
    Sub Exit_WB()
    Workbooks(location_db).Close savechanges = True
    End Sub
    

    现在,如果您想扩展并获取特定消息,我建议您将它们更改为带有输出/错误处理的函数。

    【讨论】:

    • 如上 - 我稍后会发布相同的解决方案,因此将其删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2016-04-12
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多