【问题标题】:On error along with message box in VBA与 VBA 中的消息框一起出现错误
【发布时间】:2014-09-09 08:17:17
【问题描述】:

在我的一个代码中,如果我遇到错误,我需要弹出带有错误的文件名,然后继续下一步。下面是我尝试使用的代码 sn-p,但它给了我错误。 谁能帮帮我

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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    另一种方法...

    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
    

    【讨论】:

      【解决方案2】:

      你快到了。你可以这样做:

      sourcefilename = File_list.Cells(i + 1, 1)
      On Error Resume Next 'Errors get swallowed without warning. Use sparingly.
      Set Baccha_Wbk = Wbk.Workbooks.Open(sourcefilename)
      If Err.Number <> 0 Then MsgBox ("Error in file" & sourcefilename )
      On Error Goto 0 'Back to normal: errors get thrown as usual
      

      更完整的错误处理程序可能如下所示:

      Sub abcd()
          On Error GoTo ErrorHandler
      
          '[code...]
      
          sourcefilename = File_list.Cells(i + 1, 1)
          Set Baccha_Wbk = Wbk.Workbooks.Open(sourcefilename)
      
          '[more code...]
      
      ExitProcedure:
          On Error Resume Next
          'Clean-up code goes here
          Exit Sub
      
      ErrorHandler:
          Select Case Err.Number
          'Deal with each of the expected errors
          Case 53 ' File not found
              MsgBox "File not found: " & sourcefilename
          Case 70 ' Permission denied
              MsgBox "Permission denied. Maybe you don't have permission to access this drive? " & sourcefilename
          'etc. etc.
      
          'Deal with unexpected errors
          Case Else
              UnexpectedError Err.Number, Err.Source & ", Procedure abcd of Module Module1", Err.Description, Err.HelpFile, Err.HelpContext
          End Select
          Resume ExitProcedure
          Resume
      End Sub
      

      (Why the double Resume at the end?)

      连同这个辅助子 - 随心所欲地自定义。

      Public Sub UnexpectedError(ByVal lngNumber As Long, _
                                 ByVal strSource As String, ByVal strDescription As String, _
                                 ByVal strHelpfile As String, ByVal lngHelpContext As Long)
          On Error Resume Next
          MsgBox "[" & strSource & "]" & vbCrLf & "Run-time error '" _
               & CStr(lngNumber) & "':" _
               & vbCrLf & vbCrLf & strDescription, vbExclamation, Application.Name, _
                 strHelpfile, lngHelpContext
          Application.EnableEvents = True
          Debug.Print "Case " & CStr(lngNumber) & " '" & strDescription
          'Debug.Assert False 'uncomment while developing
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-05
        • 2019-06-04
        • 1970-01-01
        • 2020-10-18
        • 2012-01-10
        • 1970-01-01
        • 2021-12-14
        • 2018-02-28
        相关资源
        最近更新 更多