错误以及VBA中的消息框 [英] On error along with message box in VBA

查看:110
本文介绍了错误以及VBA中的消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In one of my code if I get an error i need to pop the file name with error and then resume next.Below is the code snippet i am trying to use but it is giving me error. Can anyone help me

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

这篇关于错误以及VBA中的消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆