PDA

View Full Version : Error trap



garydp
09-08-2015, 11:27 PM
i have a macro that uses

On Error GoTo ErrorHandler

when an error occurs i have a variable that is populated with what section the code fails at. I was wandering is it possible to pin point the exact line of code that fails?

Aflatoon
09-09-2015, 12:10 AM
If you add line numbers to your code, you can use Erl to return the error line number.

garydp
09-09-2015, 12:58 AM
sorry how do i do that?

Aflatoon
09-09-2015, 01:09 AM
The simplest way is to use the free MZ Tools add-in. Otherwise you have to manually add numbers to the start of all the lines (like old Basic code).

garydp
09-09-2015, 01:57 AM
i didnt think vba allowed line numbers at the start. in what format should it be?

garydp
09-09-2015, 02:10 AM
its ok found it thanks

Aflatoon
09-09-2015, 04:51 AM
Simple example for others' benefit:


Sub foo() Dim n As Long

On Error GoTo err_handle

5 n = 10
10 Do
15 n = n - 1
20 Debug.Print 1 / n
25 Loop


err_handle:
MsgBox "Error " & Err.Number & ":" & vbLf & Err.Description & vbLf & "Error occurred on line # " & Erl
End Sub