I have a program which opens many Excel workbooks and merges data from all of them into a target workbook. After merging, it does some transformation of the data (column to row tranpose using custom logic) and then it adds some more columns using lookup data.
I have the following code
Sub consolidateFiles()
On Error GoTO ErrorHandler
processFiles
transform
addLookupData
Exit Sub
ErrorHandler:
Debug.Print Err.Number & " = " & Err.Description & ", Source = " & Err.Source
End Sub
Sub processFiles()
'Here I open all the files in the directory and copy and paste into the target workbook.
'If there is an error here, I would like to know the file that caused the error
'and I would like to proceed to the next file after logging the error
End Sub
Sub transform()
'Here I transform some of the data from column to year.
End Sub
Sub addLookupData()
'Here I add some new columns by looking up the data in another sheet in the target workbook. Here the lookups can return error
End Sub
I wanted to know what are the good programming practices to add error handling to my code. Right now, I have one global error handler which logs the error and continues the program. Is it a good idea to add error handling to each of the 3 sub-procedures that I am calling from my main procedure?
I read about using the undocumented ERL to get the line of error. How do we add line numbers in the VBA editor?
This is my first program in VBA. I have done a lot of Java programming, but there the error handling paradigm is very different. Hence, I would like to learn from experienced VBA programmers what are the good practices that I can follow.
I have read this excellent article which was linked in one of the questions related to VBA error handling in SO: