[VBA] Errors

Error methods:
1. GoTo
2. (Resume) Next
3. Exit Sub/Function

Ex 1

This example include first option. In our code for importig file to Excel we set all data type : .Filters.Add “All”, “*.*”

So no matter, what file will be chosen, everything will be uploaded to our worksheet.
But if only we will select the same file again we will get a run-time error 1004#
Our Procedure need to be updated.
First line will be add on this step:

If .Show = True Then
On Error GoTo SameFile
TxtFile = Dir(.SelectedItems(1))
Sheets.Add.Name = TxtFile

And then we need to write what is hidden inside SameFile by adding few lines at the end of our procedure:

SameFile:

If Err.Number = 1004 Then
MsgBox “The File you have chosen was already uploaded to this Workbook. 
Choose andother file or delete current Worksheet first”
Exit Sub
End If

For someone, who is coding error’s exception for the first time, it might be a little weird, but we can call it however we want, my idea is Same File for this example.

If you want see a whole procedure after modifiaction, click: import.