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, 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
-
Microsoft : Edge is better than Chrome
by
Alex5723
30 minutes ago -
The EU launched DNS4EU
by
Alex5723
6 hours, 4 minutes ago -
Cell Phone vs. Traditional Touchtone Phone over POTS
by
280park
4 hours, 9 minutes ago -
Lost access to all my networked drives (shares) listed in My Computer
by
lwerman
11 hours, 30 minutes ago -
Set default size for pasted photo to word
by
Cyn
17 hours, 32 minutes ago -
Dedoimedo tries 24H2…
by
Cybertooth
5 hours, 38 minutes ago -
Windows 11 Insider Preview build 27871 released to Canary
by
joep517
1 day, 16 hours ago -
Windows 11 ad from Campaign Manager in Windows 10
by
Jim McKenna
1 day, 13 hours ago -
Small desktops
by
Susan Bradley
7 hours, 11 minutes ago -
Totally disable Bitlocker
by
CWBillow
10 hours, 4 minutes ago -
Phishers extract Millions from HMRC accounts..
by
Microfix
1 day, 14 hours ago -
Windows 10 22H2 Update today (5 June) says up-to-date but last was 2025-04
by
Alan_uk
2 days, 20 hours ago -
Thoughts on Malwarebytes Scam Guard for Mobile?
by
opti1
15 hours, 13 minutes ago -
Mystical Desktop
by
CWBillow
2 days, 23 hours ago -
Meta and Yandex secretly tracked billions of Android users
by
Alex5723
2 days, 5 hours ago -
MS-DEFCON 2: Do you need that update?
by
Susan Bradley
21 hours ago -
CD/DVD drive is no longer recognized
by
WSCape Sand
3 days, 14 hours ago -
Windows 11 24H2 Default Apps stuck on Edge and Adobe Photoshop
by
MikeBravo
3 days, 17 hours ago -
North Face and Cartier customer data stolen in cyber attacks
by
Alex5723
3 days, 15 hours ago -
What is wrong with simple approach?
by
WSSpoke36
1 day, 13 hours ago -
Microsoft-Backed Builder.ai Set for Bankruptcy After Cash Seized
by
Alex5723
4 days, 3 hours ago -
Location, location, location
by
Susan Bradley
2 days, 17 hours ago -
Cannot get a task to run a restore point
by
CWBillow
4 days, 4 hours ago -
Frustrating search behavior with Outlook
by
MrJimPhelps
3 days, 19 hours ago -
June 2025 Office non-Security Updates
by
PKCano
4 days, 15 hours ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
4 hours, 19 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
4 days, 15 hours ago -
How start headers and page numbers on page 3?
by
Davidhs
5 days, 1 hour ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
4 days, 4 hours ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
5 days, 10 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.