Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On error along with message box in VBA

Tags:

excel

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
like image 233
user2786962 Avatar asked Sep 09 '14 08:09

user2786962


1 Answers

Another way to do it...

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
like image 156
Siddharth Rout Avatar answered Oct 05 '22 22:10

Siddharth Rout