Excel 错误处理-在何处输入';退出Sub';?
我有下面的代码,它只是打开一个文件。如果指定的LCO中不存在该文件,则需要显示错误消息。下面代码的问题是,当文件确实存在时,它会打开,在单元格A1中输入“Hello”,但MsgBox仍会出现。我想我把出口接头放错地方了Excel 错误处理-在何处输入';退出Sub';?,excel,vba,error-handling,Excel,Vba,Error Handling,我有下面的代码,它只是打开一个文件。如果指定的LCO中不存在该文件,则需要显示错误消息。下面代码的问题是,当文件确实存在时,它会打开,在单元格A1中输入“Hello”,但MsgBox仍会出现。我想我把出口接头放错地方了 Sub Test() Dim Location As String Dim File1 As String Dim Err1 As String On Error GoTo Err1 Location = "S:\HRIS\Restricted\Information S
Sub Test()
Dim Location As String
Dim File1 As String
Dim Err1 As String
On Error GoTo Err1
Location = "S:\HRIS\Restricted\Information Services\Regular Reports\DRS _
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
在
Err1
标签之前移动Exit Sub
:
Sub Test()
Dim Location As String
Dim File1 As String
Dim Err1 As String
On Error GoTo Err1
Location = "S:\HRIS\Restricted\Information Services\Regular Reports\DRS 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
在
Err1
标签之前移动Exit Sub
:
Sub Test()
Dim Location As String
Dim File1 As String
Dim Err1 As String
On Error GoTo Err1
Location = "S:\HRIS\Restricted\Information Services\Regular Reports\DRS 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
我一发帖就意识到了这一点。谢谢你的帮助。我一发帖就意识到了。谢谢你的帮助。在出现错误1之前,你需要一个出口接头。。。。请参见错误1…之前需要退出子系统。。。。另见