Results 1 to 10 of 10

Thread: Error Handling - Excel VBA [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Resolved Error Handling - Excel VBA [RESOLVED]

    What i want to be able to do is the following:

    i want be able to extract the line of code that is running that cause the error

    e.g if you had on line 32
    average = 32/0

    this statement would give you an error, is there anyway i could extract this line number.
    What i then want to do is to display a message box saying
    "Please contact your administrator quoting L32 and " & Err

    where L32 is the line number, and Err is obviously the error number.

    Thanks
    Mitch
    Last edited by Mitch_s_s; May 16th, 2007 at 05:23 PM.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Error Handling - Excel VBA

    To get the Line numbers, you need to add the line numbers in the code. A search here would have just shown you what you have to use
    Code:
    Public Sub ErrorLineNumber()
        On Error GoTo ErrHandler
    1:     Dim x As Integer
    2:     Dim y As Integer
    3:     x = 0
    4:     y = 10
    5:     Dim z As Integer
    6:     x = y / x
    8:     MsgBox "Succeeded"
    9:     MsgBox "Just another msgbox"
        Exit Sub
    
    ErrHandler:
        MsgBox "Error Occured at Line Number " & Erl
    End Sub
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Error Handling - Excel VBA

    Erl returns 0

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Error Handling - Excel VBA

    i removed the numbers!
    so all my subs need their lines to be numbered?

    no other way no?

    thanks by the way!

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Error Handling - Excel VBA

    You must number each line for it to work.. tho you can use an addin like MZTools (link in my signature) to automatically add them for you.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Error Handling - Excel VBA

    does this add anything to processing time?

    bit of a pain doin that to my already created sub

    might check out the add in

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Error Handling - Excel VBA

    Quote Originally Posted by Mitch_s_s
    does this add anything to processing time?
    Not noticeably.

    bit of a pain doin that to my already created sub

    might check out the add in
    The addin will do it instantly, for the entire project if you like.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Error Handling - Excel VBA

    hmm interesting,
    it's just an excel add in then, that writes to the vbproject?

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Error Handling - Excel VBA

    It a VBA (or VB) addin, which has lots of useful features.. when I first got it, it saved me about 25% of my coding effort.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: Error Handling - Excel VBA

    cheers man, might install it at home

    tight security in work, so they may not allow me to install it there!

    Thanks for this!

Posting Permissions

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



Click Here to Expand Forum to Full Width