Excel VBA Reference Line Where Error Occurs

Brass Contributor

I have searched the web over to find out how I can set my error handler to reference the line in the code that errors. I currently have it set so my error handler sends me an email with the details of the error. Which are very vague and not really helpful at pinpointing the error. So far the only solutions I see are adding line numbers myself but I do not want to do that because of many reasons: They have a whole set of rules to follow that relate only to using them. I don't want to have to re-test all of my code.

Whats frustrating to me is that there isn't something simple already in place that allows it to pick up on its own line reference number and use that to tell the user what line the error occurred on.

Another solution is to purchase third party add-in and that is not an option.

Does anyone know of a way to get this data without having to add line numbers?

5 Replies

@heylookitsme 

If your code doesn't have line numbers, VBA doesn't "know" the line number where an error occurs...

@Hans VogelaarI appologize, Let me clarify. The attached pic shows Line reference data that VBA shows when working in each module. What I'm saying is there should be a way to reference this line when errors occur.

@heylookitsme 

Yes, I know, but that information is not exposed to the programmer when an error occurs.

 

There is an obscure function Erl that returns the line number of an error, but if you haven't actually added line numbers, Erl always returns 0.

The Err object doesn't provide line number information... :(

That information is not exposed to the programmer when an error occurs.
It should be. That would provide a great way for programmers to get specific error details which would help them debug errors much faster. I see this having huge potential if it were available.

@heylookitsme 

Microsoft Access/VBA Tip: Error Handling with Line Numbers (fmsinc.com)

 

First, you must number the lines and then reference the line number that causes the error using ERL. 

You can find an example in the link above.