You're almost there. This is how you could do it:
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
A more complete error handler could look like this:
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?)
along with this helper sub — customize as you like.
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