Add / Remove Line Numbers

Top  Previous  Next

 VB.NET   VB6   VBA 

The Add / Remove Line Numbers feature allows you to add or remove line numbers to the source code of a method, class, file, project, or solution / project group. This allows you to use the Erl function of VB.NET / VB / VBA in your error handlers to know the exact line number that caused the error:


Private Sub Foo()


10:   Dim o As Object
20:   On Error GoTo Proc_error
30:   MsgBox(o.ToString)
40:   Exit Sub




50:   MsgBox("Error " & Err.Description & " in line " & Erl & " of Foo")


End Sub

Although VB.NET allows you to use structured error handling using the Try / Catch statement as follows:


Private Sub Foo()


Dim o As Object




Catch ex As Exception


End Try


End Sub

you may want to use the old error handling mechanism of VB 6.0 in the following scenarios:

  • You have migrated legacy code from VB 6.0 and you want to use the same error handling code.
  • You want to know the line that caused an error even in the Release configuration, since the ToString() method of the Exception class only shows the file name and the line number of the error in the Debug configuration (which generates a .pdb debug file). In the Release configuration the .pdb file is missing, and only the class and method name are shown in the exception message, but not the specific line number.

MZ-Tools offers the following ways to add / remove line numbers:

  • The MZ-Tools | Other Utilities | Add Line Numbers... and MZ-Tools | Other Utilities | Remove Line Numbers... menus.
  • The Add Line Numbers and Remove Line Numbers buttons on the MZ-Tools - Other Utilities toolbar.
  • In Visual Studio 2015 and higher, the keyboard shortcut associated with the MZTools.AddLineNumbers and MZTools.RemoveLineNumbers commands.
  • In Visual Studio 2013 and lower, the keyboard shortcut associated with the MZTools8.AddLineNumbers and MZTools8.RemoveLineNumbers commands.
  • In VB6 / VBA, the keyboard shortcuts associated with the Add Line Numbers / Remove Line Numbers feature.
  • In VB6 / VBA, the Add Line Numbers... / Remove Line Numbers... menu entries on the context menu of a project or file in the Project Explorer and the context menu of the code window.

When you invoke this feature:

  • If the cursor is inside a method, the feature is executed only for that method without further action.
  • Otherwise, a window is shown that allows you to select the scope of the operation (the most suitable is preselected) and to decide whether or not to keep open the modified documents:



  • Ensure that the source code can be compiled without errors before running this feature.
  • The best practice is to avoid the use of line numbers during development, because they are not needed to code and debug, they get messed with the indentation of code and you need to renumber if you add new lines of code. Instead, you should add line numbers just before making the executable (VB6 / Visual Studio) or just before distributing a copy of the Office document with VBA code to the end users. In both cases, you need to make a copy of the code with line numbers somewhere (under source control or in backup folders). You can automate the add/remove line numbers features from a script. See Automating Features.
  • Visual Basic does not allow line numbers greater than 65535 (they are treated as 0), so MZ-Tools resets the numeration when it reaches that limit. Take this into account if you use unique numbers (rather than numbers that reset at method or file level) when numbering a whole project.
  • Visual Basic does not allow a line number in the first Case statement of a Select Case block. If you try to add the line number by hand and you hit F5 to run the project, you get the VB error: "Statements and labels invalid between Select Case and first Case". So, MZ-Tools does not number the first Case statement of a Select Case block to avoid that VB error.
  • This feature doesn't support using XML literals in Visual Basic .NET. You can skip numbering some specific method defining a SkipMZToolsLineNumbering attribute somewhere in your project and applying it to the methods that you don't want to add line numbers, as shown in this example:


<System.AttributeUsage(System.AttributeTargets.All)> _

Public Class SkipMZToolsLineNumbering

Inherits System.Attribute

End Class

Public Class C1


<SkipMZToolsLineNumbering()> _

Private Sub f1()


End Sub


End Class

See Also:

Line Numbering Options

Keyboard Shortcuts Options