Error Handler #
Error Handler with a Class Module #
In VBA, where classes are not frequently used, you might wonder if it’s really necessary to implement an error handler as a class module. Still, compared to writing multiple lines of repetitive code, it might offer a slight improvement—so I decided to give it a try.
your Module #
Dim eh As New ErrorHandler
'your codes
On Error GoTo Err_Handler
'your codes
Err_Handler:
eh.CheckError
your Class Modues - ErrorHandler #
Option Explicit
Public Sub CheckError()
If Err.Number <> 0 Then
MsgBox "errNo : " & Err.Number & vbCr & _
"err : " & Err.Description, vbCritical, "Error"
Err.Clear
End If
End Sub
That said, you still end up writing around four lines of code, so in practice, the benefit may not be that significant.