use*_*962 5 excel vba excel-vba
在我的一个代码中,如果我收到错误,我需要弹出文件名错误,然后继续下一步.Below是我试图使用的代码片段,但它给了我错误.谁能帮我
sourcefilename = File_list.Cells(i + 1, 1)
Set Baccha_Wbk = Wbk.Workbooks.Open(sourcefilename)
On Error GoTo ErrMsg
ErrMsg: MsgBox ("Error in file" & sourcefilename ),On Error Resume Next
Run Code Online (Sandbox Code Playgroud)
另一种方法......
Sub Sample()
Dim sFile As String
'~~> If you are doing this in Excel. then you don't need Wbk
Dim Wbk As Excel.Application
Dim Baccha_Wbk As Workbook
Dim i As Long
On Error GoTo Whoa
'
'~~> Rest of the Code
'
sFile = File_list.Cells(i + 1, 1)
If FileFolderExists(sFile) Then
'~~> If you are doing this in Excel. then you don't need Wbk
Set Baccha_Wbk = Wbk.Workbooks.Open(sFile)
Else
MsgBox "File Doesn't exists"
End If
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
Public Function FileFolderExists(strFullPath As String) As Boolean
On Error GoTo EarlyExit
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
EarlyExit:
On Error GoTo 0
End Function
Run Code Online (Sandbox Code Playgroud)
你快到了。您可以这样做:
\n\nsourcefilename = File_list.Cells(i + 1, 1)\nOn Error Resume Next \'Errors get swallowed without warning. Use sparingly.\nSet Baccha_Wbk = Wbk.Workbooks.Open(sourcefilename)\nIf Err.Number <> 0 Then MsgBox ("Error in file" & sourcefilename )\nOn Error Goto 0 \'Back to normal: errors get thrown as usual\n
Run Code Online (Sandbox Code Playgroud)\n\n更完整的错误处理程序可能如下所示:
\n\nSub abcd()\n On Error GoTo ErrorHandler\n\n \'[code...]\n\n sourcefilename = File_list.Cells(i + 1, 1)\n Set Baccha_Wbk = Wbk.Workbooks.Open(sourcefilename)\n\n \'[more code...]\n\nExitProcedure:\n On Error Resume Next\n \'Clean-up code goes here\n Exit Sub\n\nErrorHandler:\n Select Case Err.Number\n \'Deal with each of the expected errors\n Case 53 \' File not found\n MsgBox "File not found: " & sourcefilename\n Case 70 \' Permission denied\n MsgBox "Permission denied. Maybe you don\'t have permission to access this drive? " & sourcefilename\n \'etc. etc.\n\n \'Deal with unexpected errors\n Case Else\n UnexpectedError Err.Number, Err.Source & ", Procedure abcd of Module Module1", Err.Description, Err.HelpFile, Err.HelpContext\n End Select\n Resume ExitProcedure\n Resume\nEnd Sub\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n与此辅助子 \xe2\x80\x94 一起根据您的喜好进行自定义。
\n\nPublic Sub UnexpectedError(ByVal lngNumber As Long, _\n ByVal strSource As String, ByVal strDescription As String, _\n ByVal strHelpfile As String, ByVal lngHelpContext As Long)\n On Error Resume Next\n MsgBox "[" & strSource & "]" & vbCrLf & "Run-time error \'" _\n & CStr(lngNumber) & "\':" _\n & vbCrLf & vbCrLf & strDescription, vbExclamation, Application.Name, _\n strHelpfile, lngHelpContext\n Application.EnableEvents = True\n Debug.Print "Case " & CStr(lngNumber) & " \'" & strDescription\n \'Debug.Assert False \'uncomment while developing\nEnd Sub\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
13951 次 |
最近记录: |