How can I know in which line of code generating runtime error?

G

Guest

Hi, All:

I am debugging a program, there is always runtime errors. I spent a lot of
time to debug them. I check the err object, e.g. err.description, they do not
provide which line of code generating the error.
Your help is appreciated..........

Pascal
 
S

Stefan Hoffmann

hi Pascal,

Pascal.H said:
I am debugging a program, there is always runtime errors. I spent a lot of
time to debug them. I check the err object, e.g. err.description, they do not
provide which line of code generating the error.
Your help is appreciated..........
You can use Erl() to retrieve the classic line number, but you have to
number your code lines before using it, otherwise it returns 0.

You may use http://mztools.com for VBA. This can number selected code
automatically.


mfG
--> stefan <--
 
B

Bill Manville

If you have no error handler in the procedure which gets the error then
the error message has a Debug button. Click on it and you will be taken
to the line in question, highlighted in yellow.

If you have an error handler in the procedure which gets the error then
it can use Resume to go back to the line which caused the error.
I use a central error handler to report and log the error, but the
local error handler equivalent to what I do is

Sub DoSomething()
On Error Goto locErr
'... do some stuff
V=1/0 ' get an error
'... do some more stuff
TidyUp:
Exit Sub
locErr:
Select Case MsgBox("Error " & Err.Number & ": " & Err.Description,
vbAbortRetryIgnore)
Case Retry
Stop ' after selecting retry, F8 twice to get to erroneous line
Resume
Case Abort
Resume TidyUp
Case Ignore
Resume Next ' if it makes sense to do so.
End Select
End Sub

Bill Manville
MVP - Microsoft Excel, Oxford, England
 
G

Guest

Another possibility is to turn error trapping off during development. In the
VBA editor Tools, Options, General tab and select break on all errors. This
technique allows you to see the the current value of variables, etc. to help
with the debug process.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top