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, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
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
-
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
5 hours, 45 minutes ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
1 hour, 47 minutes ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
8 hours, 11 minutes ago -
Return of the brain dead FF sidebar
by
EricB
1 hour, 4 minutes ago -
windows settings managed by your organization
by
WSDavidO61
2 hours, 53 minutes ago -
Securing Laptop for Trustee Administrattor
by
PeachesP
3 hours, 6 minutes ago -
The local account tax
by
Susan Bradley
21 minutes ago -
Recall is back with KB5055627(OS Build 26100.3915) Preview
by
Alex5723
14 hours, 47 minutes ago -
Digital TV Antenna Recommendation
by
Win7and10
7 hours, 19 minutes ago -
Server 2019 Domain Controllers broken by updates
by
MP Support
1 day, 2 hours ago -
Google won’t remove 3rd party cookies in Chrome as promised
by
Alex5723
1 day, 4 hours ago -
Microsoft Manager Says macOS Is Better Than Windows 11
by
Alex5723
1 day, 7 hours ago -
Outlook (NEW) Getting really Pushy
by
RetiredGeek
9 hours, 57 minutes ago -
Steps to take before updating to 24H2
by
Susan Bradley
46 minutes ago -
Which Web browser is the most secure for 2025?
by
B. Livingston
14 hours, 23 minutes ago -
Replacing Skype
by
Peter Deegan
2 hours, 56 minutes ago -
FileOptimizer — Over 90 tools working together to squish your files
by
Deanna McElveen
1 day, 1 hour ago -
Excel Macro — ask for filename to be saved
by
nhsj
38 minutes ago -
Trying to backup Win 10 computer to iCloud
by
SheltieMom
2 hours, 43 minutes ago -
Windows 11 Insider Preview build 26200.5570 released to DEV
by
joep517
3 days, 7 hours ago -
Windows 11 Insider Preview build 26120.3941 (24H2) released to BETA
by
joep517
3 days, 9 hours ago -
Windows 11 Insider Preview Build 22635.5305 (23H2) released to BETA
by
joep517
3 days, 9 hours ago -
No April cumulative update for Win 11 23H2?
by
Peobody
1 day, 21 hours ago -
AugLoop.All (TEST Augmentation Loop MSIT)
by
LarryK
3 days, 9 hours ago -
Boot Sequence for Dell Optiplex 7070 Tower
by
Serge Carniol
4 days ago -
OTT Upgrade Windows 11 to 24H2 on Unsupported Hardware
by
bbearren
4 days, 4 hours ago -
Inetpub can be tricked
by
Susan Bradley
2 days, 11 hours ago -
How merge Outlook 2016 .pst file w/into newly created Outlook 2024 install .pst?
by
Tex265
2 days, 22 hours ago -
FBI 2024 Internet Crime Report
by
Alex5723
4 days, 8 hours ago -
Perplexity CEO says its browser will track everything users do online
by
Alex5723
1 day, 17 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.