Results 1 to 10 of 52

Thread: Resume On Error GoTo 0 -1 GoTo Error Handling Statements Runtime VBA Err Handling ORNeRe GoRoT N0Nula 1

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    Part 1) Err object ... Err.Raise

    Part 1) Err object. Err.Raise.
    The basic command for a custom error handler is the use of the Err object method of .Raise in such a code line:
    Err.Raise(Number:= , Source:= , Description:= , HelpFile:= , HelpContext:= , LastDllError:= )
    The basic function and usual usage is to start the default VBA Error handler, but the text properties will be mostly left empty in the uses of it usually seen.
    I say mostly as syntaxly you must give at least the first, Number:= in the above code line if you use it.

    Possibly the Err.Raise Custom Error handler has been introduced by someone for fun as some sort of Trolling. Most of what you can do with it you can do in other more intuitive ways. In addition there are a few quirks that no one quite seems to understand.


    So the basic idea is that you can force something similar to happen as to that when an error occurs. Similar…

    I think if we take another look generally at the Err object, try to work through what it, and in particular its Method .Raise does, mention a few quirks along the way, … then I think we will see that .Raise is not much more than a complicated way to bring up a Message box.

    Back to the start:
    Three codes did a demo on what goes on if you try to divide by zero. Here the simplest again:
    Code:
    Sub Error_VBADefaultErrorHandling()
    Dim Db As Double
     Let Db = 1 / 0 ' Code terminates here and VBA chucks up a meassage box
    'You never get here
    End Sub
    The result with those codes was an error, and the corresponding message was of the form:

    Laufzeitfehler '11':
    Division durch Null

    Runtime Error '11':
    division with zero




    The following codes are not much use for anything over than the discussions here. They are not much use as the main result of a simple call of the Err.Raise will be to stop the code in the typical VBA default error handling way.
    The slight difference is that VBA does not make an attempt to fully fill the Properties of the Err object. This is reasonable as it has no real error to refer to.
    So the syntax of the method allows for Property entries, with a couple of quirks:
    A number must be give;.
    If the number happens to be a number VBA recognises then VBA will add the appropriate other information
    Code:
    Sub RaiseAnyNumber()
     Err.Raise Number:=9999
    End Sub
    ErrRaise9999Help.JPG : https://imgur.com/KSlN6D7 https://i.postimg.cc/sX8dmqY0/Err-Raise9999-Help.jpg
    https://i.postimg.cc/fbzNRxW5/Err-Raise9999-Help.jpg

    ErrRaise9999Help.JPGErrRaise9999 Help.jpg

    If we use the number 11 then VBA recognises that as the error when trying to divide by zero, and adds appropriately the description
    Code:
    Sub Raise11()
     Err.Raise Number:=11
    End Sub
    ErrRaise11Help.JPG : https://imgur.com/tL6uvxN https://i.postimg.cc/R0gz6NNm/Err-Raise11-Help.jpg
    ErrRaise11Help.jpg

    We can overwrite the attempt from VBA to add the corresponding information to the Err object Properties, although it appears that number is still used by VBA
    Code:
    Sub Raise11B()
     Err.Raise Number:=11, Description:="An Error with Number 11, Bollox"
    End Sub
    ErrRaise11BolloxHelp.JPG : https://imgur.com/tDK7JwF https://i.postimg.cc/t4Hb8KLK/Err-Ra...ollox-Help.jpg
    ErrRaise11BolloxHelp.jpg






    _.......continued in next post
    Last edited by DocAElstein; 03-24-2023 at 09:16 PM.

Similar Threads

  1. Replies: 8
    Last Post: 09-01-2015, 01:50 AM
  2. Difference Between 'On Error GoTo 0' And 'On Error GoTo -1'
    By Transformer in forum Familiar with Commands and Formulas
    Replies: 7
    Last Post: 07-02-2015, 04:07 PM
  3. Replies: 2
    Last Post: 05-14-2013, 01:02 AM
  4. Runtime Error 481 invalid figure when PNG
    By Tony in forum Excel Help
    Replies: 0
    Last Post: 02-12-2013, 12:59 AM
  5. Replies: 10
    Last Post: 04-07-2012, 05:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •