Last active
February 13, 2016 22:59
-
-
Save mrowles/4263044 to your computer and use it in GitHub Desktop.
Microsoft Excel: Central Error Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Global Error Varibles | |
Public bDisplayError As Boolean | |
Public sProcName, sDetails As String | |
Public iErrorCount As Integer | |
Public Function bCentralErrorHandler(ByVal sModule$, ByVal sSub$, ByVal sDetails$) As Boolean | |
'Make sure there is no errors in the Error Handling system | |
On Error Resume Next | |
Dim sFullSource, sPath, sFile As String | |
sFile = ThisWorkbook.Name | |
sPath = ThisWorkbook.Path | |
'Make sure the log path is valid | |
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\" | |
'Get the file the error came from, the module, and a procedure description | |
sFullSource = "[" & sFile & "]" & " Module=" & sModule & " Procedure=" & sSub & " " | |
'Log the error to a file | |
'Please update [PROJECTNAME] | |
Open sPath & "PROJECTNAME Runtime Error - " & Application.UserName & ".log" For Append As #1 | |
Print #1, Application.UserName & " | " & sFullSource & Format$(Now(), " dd/mm/yyyy hh:mm AM/PM") & vbCrLf & " " & sDetails | |
Close #1 | |
'MsgBox sDetails | |
iErrorCount = iErrorCount + 1 | |
'Clear errors | |
Err.Clear | |
'Reset the error handler | |
bCentralErrorHandler = False | |
End Function | |
'Using the Central Error Handler | |
'Please update [MODULENAME] & [SUB/FUNCTIONNAME] | |
Sub SubExample | |
On Error GoTo ErrHandler | |
'Sub content here | |
'Exit the sub at the end so ErrHandler isn't run unnecessarily | |
Exit Sub | |
ErrHandler: | |
sDetails = "Error Number = " & CStr(Err.Number) & " Error Description = " & Err.Description | |
If bCentralErrorHandler("MODULENAME", "SUB/FUNCTIONNAME", sDetails) Then | |
End If | |
Resume Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment