1

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.

1
  • What you should probably do here is post the actual error and de-bugging line you receive upon the "error that users receives". Commented Jun 15, 2018 at 9:25

1 Answer 1

2

As I've just had a similar problem, here's my solution to it:

Check the on-error behavior of your (or the executing computer's) VBE. Not sure about the correct names in English Office, but it should be along the lines of Tools, Options, tab General. There, on the right side are 3 radio buttons:

Break on:

  • every error
  • error in class modules
  • unhandled errors

If the first one is selected, that's the root cause of your issues. It will lead to every error raising an error dialog, regardless of it being handled or not.
The third option is not very useful when developing classes, as when it's activated and you click on Debug in an error dialog, it will only ever take you to the entrance point into the class module. So there are possible quite a few lines of code you'll have to step through manually (F8) before you reach the actual line generating the error.


A potential problem this setting (Break on every error) can create in a production setting: It circumvents every single bit of error handling you might have in place. If for some reason, a user/client has this setting enabled, your application will not be usable. And a quick search (Google/stackoverflow) suggests that the setting can't be effectively changed at runtime. (At least not programmatically.) The only ways to change it are (a) to have the user change it or (b) to change the respective registry entry and restart the VBA host application (Excel in this case).

Sign up to request clarification or add additional context in comments.

5 Comments

Sooo simple but I never payed attention to the second option. My F8 key will be relieved now :-)
@PatrickHonorez At one point I (unknowingly) had the setting differently for Excel and Access, which I both frequently use. And I thought I was going crazy with one of them requiring me to step into the class code all the time. Took a while to figure out why that was. :D
Hi lnarion. My issue is not connected with VBE and debugging. This is more VBA behaviour. When SubA has error handing and it calls SubB and there is an error in SubB (which doesn't have error handing), VBA normally jump to error handing in SubA. However here, in my example above, it doesn't do that.
I have just checked with setting on the 3rd line "Break on Unhandled Errors". Now error handing functions well.
@KamilCzapnik You're right. I've never consciously observed that specific behavior, but can absolutely reproduce what you're saying. So there is a difference in behavior between "ordinary" procedures and class procedures, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.