Wednesday, March 16, 2011

Excel 2000 VBA: Errors Raised within Class Debug As If Raised at Property Call

I am (unfortunately) developing an application in Excel 2000 VBA. I believe I have discovered that any error raised within a Custom Class property, function, or sub debugs as if the error were raised at the point in the VBA code where the property is called. That is, the VBE debugger does not take me to the point in the Class property where the error occurred, but instead where the property was first entered (from a Module Sub or Function, e.g.) This makes it frustrating to develop anything more than the most shallow OO Excel 2000 VBA code since I have to step line-by-line through every Class method to discover the instructions causing an error.

Am I missing something or is this a known bug I have to deal with in Excel 2000? Has this been fixed in 2003 or 2007?

Example code:

'''''''''''''''
'In Module1:

Public Sub TestSub1()
    Dim testClass As Class1
    Dim testVariant As Variant
    Set testClass = New Class1
    testVariant = testClass.Property1 'Debugger takes me here...
End Sub

''''''''''''''
' In Class1

Property Get Property1() As Variant
    Err.Raise 666, , "Excel 2000 VBA Sux!" 'But error is actually thrown here.
End Property
From stackoverflow
  • This "feature" is the same in Excel 2003 and I'd be surprised if it's different in 2007.

    DGGenuine : This 'feature' sounds more like a ploy to encourage Visual Studio purchases. Le sigh. Thanks!
    dbb : I think Microsoft has deliberately neglected VBA for about 10 years, in favour of .Net etc. They just don't get the value that VBA adds in everyday business.
    Mike Woodhouse : It's the same feature. It's the same code in fact. Has been for about a decade now. It seems there is such a thing as too much stability.
  • For Office 2003 you will get this behaviour when the debugger is configured to break on unhandled errors (the default configuration).

    If you want it to break on the Err.Raise line, you need to configure it to break on all errors (Tools/Options/General/Error Trapping/Break on All Errors).

    I believe it's the same for Office 2000 but don't have a copy to check.

    DGGenuine : Thanks! Actually of the three options (Break on All, Break in Class, and Break on Unhandled Errors) Break in Class is what I think I was looking for.
  • This page is a very good resource on error handling in VBA:

0 comments:

Post a Comment