I am trying to debug my excel macro but i am having a hard time because the “Erl” feature doesn’t work unless code line numbers have been added. Is there a quick way to add code line numbers (aside from typing them in manually)? Thanks for any responses!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
line numbers in VBA code
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » line numbers in VBA code
- This topic has 6 replies, 6 voices, and was last updated 9 years, 5 months ago.
AuthorTopicWSmikerandell93
AskWoody LoungerOctober 29, 2015 at 12:15 pm #502888Viewing 5 reply threadsAuthorReplies-
RetiredGeek
AskWoody_MVPOctober 29, 2015 at 12:33 pm #1535010Mike,
Welcome to the Lounge as a new poster. :cheers:
Take a look at this macro.
I would recommend using this on a COPY of your workbook so you can maintain the original un-numbered code.
HTH :cheers:
-
WSvincetortello
AskWoody LoungerOctober 29, 2015 at 6:43 pm #1535067Hi Mike,
By default, I don’t think there’s any quick way to add line numbers. There are code-editing items out there that can do that sort of thing if you google it, one is called codeliner or something like that. Other than that, you could try writing a macro that will do it by using the ReplaceLine method of the CodeModule object. I hope this helps! -
Maudibe
AskWoody_MVPNovember 21, 2015 at 11:49 pm #1538614Mike,
Vince is correct. The following code will add the line numbers directly into the code without copy/pasting. Replace the module or form and procedure names (in blue) with those in your project. It will take into consideration where the procedure is located and any declarations in the declarations section of the module. It will exclude any lines that cannot have a line number and add only one line number to a code line than spans multiple lines.
HTH,
MaudSample code prior to running “AddLineNums”:
42718-Mike1Sample code after running “AddLineNums”:
42719-Mike2Code:Public Sub AddLineNums() [COLOR=”#008000″]’————————————– ‘DECLARE AND SET VARIABLES[/COLOR] Dim Procedure As CodeModule, Code As String, Module As String, Proc As String Dim StartLine As Long, EndLine As Long, LineNum As Integer, I As Long [COLOR=”#0000FF”]Module = “Module1”[/COLOR] [COLOR=”#008000″]’CHANGE TO MODULE NAME[/COLOR] [COLOR=”#0000FF”]Proc = “UpdateStats” [/COLOR] [COLOR=”#008000″]’CHANGE TO PROCEDURE NAME[/COLOR] Set Procedure = ThisWorkbook.VBProject.VBComponents(Module).CodeModule StartLine = Procedure.ProcBodyLine(Proc, vbext_pk_Proc) EndLine = Procedure.ProcCountLines(Proc, vbext_pk_Proc) – StartLine LineNum = 10 [COLOR=”#008000″]’————————————– ‘CYCLE THROUGH LINES OF CODE[/COLOR] For I = StartLine To EndLine [COLOR=”#008000″]’————————————– ‘EXCLUSIONS[/COLOR] If InStr(1, Procedure.Lines(I, 1), “Sub”, vbTextCompare) > 0 Then GoTo NextLine If InStr(1, Procedure.Lines(I, 1), “Function”, vbTextCompare) > 0 Then GoTo NextLine If I > 1 Then If Right(Procedure.Lines(I – 1, 1), 1) = “_” Then GoTo NextLine End If [COLOR=”#008000″]’————————————– ‘ADD LINE NUMBER[/COLOR] Procedure.ReplaceLine I, LineNum & Chr(9) & Procedure.Lines(I, 1) LineNum = LineNum + 10 NextLine: Next I [COLOR=”#008000″]’————————————– ‘CLEANUP[/COLOR] Set Procedure = Nothing End Sub
-
Maudibe
AskWoody_MVPNovember 22, 2015 at 3:04 am #1538643Mike,
I forgot to mention that you have to reference the Microsoft Visual Basic for Applications Extensibility 5.3 library.
Alt-F11 to open VB editor > Tools > Reference… > scroll down to Microsoft Visual Basic for Applications Extensibility 5.3 library > Tick the checkbox next to it > OK.
Maud
-
kdock
AskWoody Plus -
WSHiTechCoach
AskWoody LoungerNovember 23, 2015 at 1:42 pm #1538862I will second the MZ-Tools (Click Here) recommendation. Many VBA developers I know say it is a MUST HAVE for anyone working in VBA (Word Macros).
The great thing about MZ-Tools is that is a VBE add-in which allows it to work in ALL Office applications that use the VBE editor for VBA code. It even works across multiple version of Office on the same PC.
Viewing 5 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
1 minute ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
2 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
7 hours, 3 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
8 hours, 10 minutes ago -
0Patch, where to begin
by
cassel23
2 hours, 12 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
21 hours, 47 minutes ago -
89 million Steam account details just got leaked,
by
Alex5723
9 hours, 33 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 6 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
21 hours, 19 minutes ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
8 hours, 27 minutes ago -
Installer program can’t read my registry
by
Peobody
3 hours, 24 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
19 hours, 6 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 2 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 17 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 2 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 3 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 7 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 8 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 12 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 18 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 17 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
15 hours, 10 minutes ago -
May 2025 updates are out
by
Susan Bradley
44 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
2 days, 23 hours ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
2 days, 23 hours ago -
Drivers suggested via Windows Update
by
Tex265
2 days, 23 hours ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
19 hours, 46 minutes ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
3 days, 6 hours ago -
Apple releases 18.5
by
Susan Bradley
3 days, 1 hour ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.