在我的一个代码中,如果我遇到错误,我需要弹出错误的文件名,然后继续执行下一步。以下是我尝试使用的代码片段,但这给了我错误。
谁能帮我

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

最佳答案

另一种方式...

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

07-26 02:07