You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- VBA Error Trapping

Advertisement

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.

Phil

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 PROC_ERR: 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.

Source: http://www.fmsinc.com/free/NewTips/VBA/ErrorHandling/LineNumber.html

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | Thanks, ERL was exactly what I needed. And I'll check out your reference materials too. Phil |

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

Excel

Answers by Expert:

Excel Formulas, macros, automation. Microsoft Excel MVP - 2010. Code site with free code snippets and techniques: http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/files

Microsoft Excel MVP - 2010. I have my own extensive Excel help/code site: http://sites.madrocketscientist.com/jerrybeaucaires-excelassistant/files
=====================
I have been offering free assistance as an Excel aid on many web sites for many years:
(http://www.excelforum.com - JBeaucaire) ======== (http://www.askmehelpdesk.com/spreadsheets - JBeaucaire) ======= (http://www.mrexcel.com/forum - jbeaucaire)**Education/Credentials**

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

Microsoft Excel MVP 2010