错误处理 - 在哪里输入"退出子"?



>我有下面的代码,它只是打开一个文件。如果指定的文件中不存在文件,那么我需要显示一条错误消息。我在使用以下代码时遇到的麻烦是,当文件确实存在时,它会打开,在单元格 A1 中输入"Hello",但 MsgBox 仍然出现。我想我把出口子放在错误的地方??

Sub Test()
Dim Location As String
Dim File1 As String
Dim Err1 As String
On Error GoTo Err1
Location = "S:HRISRestrictedInformation ServicesRegular ReportsDRS _   
Automation" & Format(Date, "DD.MM.YYYY")
File1 = "Test.xlsx"
Workbooks.Open FileName:=Location & File1
Range("A1").Value = "Hello"
Err1:
MsgBox "Could not Locate " & Location & File1
Exit Sub
End Sub

Exit Sub移动到标签之前Err1

Sub Test()
Dim Location As String
Dim File1 As String
Dim Err1 As String
On Error GoTo Err1
Location = "S:HRISRestrictedInformation ServicesRegular ReportsDRS Automation" & Format(Date, "DD.MM.YYYY")
File1 = "Test.xlsx"
Workbooks.Open Filename:=Location & File1
Range("A1").Value = "Hello"
Exit Sub
Err1:
MsgBox "Could not Locate " & Location & File1
End Sub

最新更新