You are here:

Excel/VBA Error Trapping


I use Office 2013, Excel 15.0.4841.1000 and write some large Macros for it.
I number macro lines to help in debugging and I know how to use "On Error".
Please help me trap the line number for the error.  I have see the term "errLine" but it seems to be a user defined function.

Thanks for your help.

ERL Function
Less well-known is the ERL function which gives you the line number where an error occurs. Most developers don't use this because this function returns 0 if the crashing line isn't numbered.

The trick is to number all your lines before you deploy your application to your users. If you do that, the ERL function identifies exactly where a crash occurs:

Sub SampleErrorWithLineNumbers()

     Dim dblNum As Double
     Dim dblRnd As Double

10   On Error GoTo PROC_ERR

     ' Randomly crashes on a line below:
20   dblRnd = Rnd()

30   Select Case dblRnd
       Case Is < 0.2
40       dblNum = 5 / 0
50     Case Is < 0.4
60       dblNum = 5 / 0
70     Case Is < 0.6
80       dblNum = 5 / 0
90     Case Is < 0.8
100      dblNum = 5 / 0
110    Case Else
120      dblNum = 5 / 0
     End Select

130  Exit Sub

140  MsgBox "Value: " & dblRnd & vbCrLf & _
            "Error Line: " & Erl & vbCrLf & _
            "Error: (" & Err.Number & ") " & Err.Description, vbCritical
End Sub

In the silly example above all the dblNum equations crash because of division by zero. The Rnd function gives a random number between 0 and 1, and based on the value dies on one of the lines in the Select Case statement. The ERL function identifies which line and displays a message box

"Division by zero error"

In this case, the value was 0.57, and we see it dies in the section < 0.6.

Obviously, this is just an example of how ERL works. In a real application, we'd have a global error handler invoked in the PROC_ERR section, where the error information would be written to a file along with any other information about the environment.

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Jerry Beaucaire


Excel Formulas, macros, automation. Microsoft Excel MVP - 2010. Code site with free code snippets and techniques:


Microsoft Excel MVP - 2010. I have my own extensive Excel help/code site: ===================== I have been offering free assistance as an Excel aid on many web sites for many years: ( - JBeaucaire) ======== ( - JBeaucaire) ======= ( - jbeaucaire)

Bachelor's Degree from Azusa Pacific University in Mathematics and Music Composition

Awards and Honors
Microsoft Excel MVP 2010

©2017 All rights reserved.