Find Error Line Number in VBA

user2864154

I'm trying to find the line number where my code crashes but many explanation on this site seems to complicated for my level.

My code is basically as below and I have no idea where it's breaking.

Sub1   
    Call function1  
    Call function2  
End Sub  

Other answers on this website seems to be just a short function. But I don't know where to call the function in my code or how to get a popup message. If I'm meant to put my sub1 code into their function, I don't know where either. Beginner here.

Mathieu Guindon

If your code doesn't have line numbers, then VBA has no way of giving you line numbers.

You can write VBA and make it look 1980-like to do this:

Sub1
On Error GoTo 100
10   Call Function1
20   Call Function2
90   Exit Sub
100  Debug.Print Err.Message & " on line " & Erl
End Sub

But you don't want to do that. Really, you don't need a line number.

You need smaller functions that handle runtime errors.

On Error GoTo ErrHandler

When a runtime error occurs, execution jumps to the line label called ErrHandler.

     ...
     Exit Sub
ErrHandler: '<< the line label is denoted with a colon

What goes in that handler? If you're debugging, you might want to just Stop execution there and inspect your locals:

    Stop

Then add Resume on the next line, and press F8 to step into it. Resume will return to the call that caused the error. If that's a function call, then you need to handle runtime errors in that function.

Make sure you never leave Stop and Resume instructions in production code:

Sub WhenWillThisEnd()
    On Error GoTo ErrHandler
    Debug.Print 42/0
    Exit Sub
ErrHandler:
    Resume 'jumps back to the line that caused the error
    Resume Next 'resumes execution on the line right after the one that went boom
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find line number and file name of error

From Dev

Trick to quickly find file & line number throwing an error in Go?

From Dev

How do I find the line number in Bash when an error occured?

From Dev

Find to Json line number

From Dev

Bash find line number

From Dev

VBA How to find closest number

From Dev

Find the line number of console application?

From Dev

Find the line number of console application?

From Dev

Find a string and display the line number and the line itself?

From Dev

Find word in line and count number of line

From Dev

VBA Error 1004 number conversion

From Dev

Get error line number Python

From Dev

rspec not reporting line number of error

From Dev

Show Line Number in Error Message

From Dev

How to jump to line number in VBA editor?

From Dev

VBA - Argument Not Optional Error With .Find

From Dev

how to find last visible column number in vba?

From Dev

How to find a number between two characters in VBA

From Dev

Returning number of results FIND method (vba)

From Dev

How to find a number after a character in VBA

From Dev

Returning number of results FIND method (vba)

From Dev

How to find the line number of the end of file in python?

From Dev

Find number in the next line that matches the regular expression

From Dev

Golang: find string in file and show line number

From Dev

SQL Find the number of orders in each line count

From Dev

Listing line number of results in `find` and `ls`

From Dev

Find first occurence of a number in each line of a file

From Dev

Is there a way to find the line number of the current line being read from a file?

From Dev

find and print particular line from list of filename and line number

Related Related

HotTag

Archive