Add line numbers to larger procedures for easier locating of errors

Line numbers in VBA are optional, but they become powerful when paired with error handling, debugging, and logging - notably in larger procedures. Line numbers let you pinpoint exactly where an error occurred. Without line numbers, using VBA can only tell you the procedure name. With line numbers, you can log the exact error line by using the Erl function


    Set app = Nothing
HandleExit:
    Exit Sub
HandleError:
    HandleError Err, ErrLine:=Erl(), FeedbackType:=eErrorFeedbackTypeReportableMessage, _
        Module:="Logger", Procedure:="CreateLogTable", ExtraInfo:=""
    Resume HandleExit
End Sub
menu add line numbers

Adding line numbers using the Code Explorer

You can add (or remove) line numbers to a selected procedure from the right-click menu in the Code Explorer. Basicly you start from the , right-click a module and check 'add line numbers'.


130 For Each tdf In db.TableDefs
140     If StrComp(tdf.Name, TableName, vbTextCompare) = 0 Then 'string1 is equal to string2
150         LogTableIsPresent = True
            Exit Sub
        End If
    Next

Remove line numbers

If you change your mind, you can remove line numbers the same way.

Set line numbering preferences

Two preferences allow you to customize line numbering (Code VBA menu » Preferences » Indentation and line numbers.)

Start line numbering at 100

Starting line numbering at 100 has the advantage that in general all lines will start with a 3 character number, making code easier to read because lines better align.

Make line numbers increment 10

Using an increment of 10 allows for easier adding of extra lines with their own line numbers when changing code later.