I'm having difficulty with error handling in class modules.
My application is set up the following way.
There is an ActiveX object (lets call it Lable1) on Excel sheet, that has assigned to it a class with events (ClassA). When there is a mouseup event fired, that class launches a procedure (SubB).
SubB initialises ClassC, which sometimes can produce an run time error during initializing. So I have introduced error handling in ClassA, which starts every user subroutine, so the error can bubble up to the top and get handled there (a log will be uploaded, so the dev team can look at it).
The issue I'm having is that the error handing is not working. The user is still getting Run-time error dialog box, instead of clean error handing.
Here is example code:
In Sheet1 create Label1 (activeX) and add following code:
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
If ButtonToClick Is Nothing Then
Set ButtonToClick = New ClassA
Set ButtonToClick.ButtonLabel = Label1
End If
End Sub
Create ClassA and add:
Public WithEvents ButtonLabel As MSForms.Label
Private Sub ButtonLabel_MouseUp(ByVal MouseButton As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
On Error GoTo EH
SubB
Exit Sub
EH:
MsgBox "OMG AN ERROR!"
End Sub
Create Module1 and add:
Public ButtonToClick As ClassA
Sub SubB()
Dim WhyTho As ClassC
Set WhyTho = New ClassC
End Sub
Create ClassC and add:
Private Sub Class_Initialize()
Dim i As Long
i = 7 / 0
End Sub
The error should be handled, the code should jump to EH: in ButtonLabel_MouseUp, but I still get the run-time dialog box instead. What I am doing wrong?
If this is a limitation of VBA, what should I do to get around it?
The issue I was experiencing is connected to VBE settings. The "Break in Class Module" is causing the error handling in ClassA / SubB to be ignored in ClassC.