Return the Line Number where the Error Occured

R

RyanH

Is there a way to return the Line Number where an error occured in a module?

I currently have this and I think it would be useful to know the error
location.

Sub TEST()

On Error GoTo ErrorHandler

Const ModuleName As String = "Module1"
Const SubName As String = "TEST"

' my code here
Exit Sub

'*****************
ErrorHandler:
Call ErrorHandler(ModuleName, SubName, "Error Line Location Here")

End Sub
 
D

Daniel.C

You can use "erl" if your code lines are numbered :
14 On Error Resume Next
15 Workbooks.Open "NotAvalidOne.xls"
16 MsgBox Erl
 
B

Bob Phillips

Just to add to Daniel's response, you don't need to add line numbers to
every line. If you just scatter them every so often, it will report the last
line number it encountered as the offending line. You could then home in on
the actual line.

There is also a free VBE addin, MZ-Tools, that, amongst other things, adds
and removes line numbers to and from your code. You can get it at
http://www.mztools.com/v3/download.aspx

--
__________________________________
HTH

Bob

Daniel.C said:
You can use "erl" if your code lines are numbered :
14 On Error Resume Next
15 Workbooks.Open "NotAvalidOne.xls"
16 MsgBox Erl
 
R

RyanH

how do you number your lines?
--
Cheers,
Ryan


Daniel.C said:
You can use "erl" if your code lines are numbered :
14 On Error Resume Next
15 Workbooks.Open "NotAvalidOne.xls"
16 MsgBox Erl
 
R

RyanH

Wht is the advantage to having line numbers? Is this the only case you would
need line numbers?
--
Cheers,
Ryan


Bob Phillips said:
Just to add to Daniel's response, you don't need to add line numbers to
every line. If you just scatter them every so often, it will report the last
line number it encountered as the offending line. You could then home in on
the actual line.

There is also a free VBE addin, MZ-Tools, that, amongst other things, adds
and removes line numbers to and from your code. You can get it at
http://www.mztools.com/v3/download.aspx
 
D

Daniel.C

Manually, unless you use the addin indicated by Bob. As far as I know,
numbering lines has no great use, but certainly, the addin should have other
interesting functionalities.
Daniel
RyanH said:
how do you number your lines?
 

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