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!
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
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, 6 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 27868 released to Canary
by
joep517
1 hour, 46 minutes ago -
X Suspends Encrypted DMs
by
Alex5723
3 hours, 58 minutes ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
4 hours, 16 minutes ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
4 hours, 53 minutes ago -
OpenAI model sabotages shutdown code
by
Cybertooth
5 hours, 30 minutes ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
48 minutes ago -
Enabling Secureboot
by
ITguy
40 minutes ago -
Windows hosting exposes additional bugs
by
Susan Bradley
13 hours, 24 minutes ago -
No more rounded corners??
by
CWBillow
9 hours, 13 minutes ago -
Android 15 and IPV6
by
Win7and10
1 hour, 5 minutes ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
1 day, 1 hour ago -
T-Mobile’s T-Life App has a “Screen Recording Tool” Turned on
by
Alex5723
1 day, 4 hours ago -
Windows 11 Insider Preview Build 26100.4202 (24H2) released to Release Preview
by
joep517
23 hours, 2 minutes ago -
Windows Update orchestration platform to update all software
by
Alex5723
1 day, 11 hours ago -
May preview updates
by
Susan Bradley
23 hours, 10 minutes ago -
Microsoft releases KB5061977 Windows 11 24H2, Server 2025 emergency out of band
by
Alex5723
14 hours, 45 minutes ago -
Just got this pop-up page while browsing
by
Alex5723
1 day, 3 hours ago -
KB5058379 / KB 5061768 Failures
by
crown
1 day, 1 hour ago -
Windows 10 23H2 Good to Update to ?
by
jkitc
3 hours, 13 minutes ago -
At last – installation of 24H2
by
Botswana12
2 days, 3 hours ago -
MS-DEFCON 4: As good as it gets
by
Susan Bradley
19 seconds ago -
RyTuneX optimize Windows 10/11 tool
by
Alex5723
2 days, 15 hours ago -
Can I just update from Win11 22H2 to 23H2?
by
Dave Easley
13 hours, 56 minutes ago -
Limited account permission error related to Windows Update
by
gtd12345
3 days, 4 hours ago -
Another test post
by
gtd12345
3 days, 5 hours ago -
Connect to someone else computer
by
wadeer
2 days, 23 hours ago -
Limit on User names?
by
CWBillow
3 days, 2 hours ago -
Choose the right apps for traveling
by
Peter Deegan
2 days, 16 hours ago -
BitLocker rears its head
by
Susan Bradley
2 days ago -
Who are you? (2025 edition)
by
Will Fastie
1 day, 23 hours 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.