code line numbers VBA

iknowu99

Well-known Member
Joined
Dec 26, 2004
Messages
1,158
Office Version
  1. 2016
Hi All reading this,

Can't figure out if there is a way to see line numbers in Modules/code?

Gracias
 
The feature is in vb.net which I like. Is there a reason why you need the numbers?

You can copy and paste to a sheet if you need a quick view of them.

Along the lines of this suggestion, you can use a lightweight editor (I use Notepad++ which is freeware) to display line numbers, on screen and in print. If your program actually needs to reference (i.e. GoTo) specific lines of code (a la GW Basic), you can use mikerikson's tool, which works great but not a good general practice in the VB programming paradigm.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The point of line numbers as referenced in previous replies: "They do have one benefit - you can use Erl in an error handler to tell you which line the error occurred on." I also like line numbers for visual reference when coding.
 
Upvote 0
FYI the MZTools add-in has an option to add or remove line numbers from your code.
 
Upvote 0
Thks, I'll check it out. Have changed code to do away with "L" and colon - forgot that you can have straight line numbers.
Code:
Option Compare Text


Private Sub AddLineNumbers_Click()
 
Set sh = Sheet4
ctr = 0
                                        
For i = 7 To 1000
    If sh.Cells(i, 1) = "" Then GoTo Next_i ' find start of code
    If sh.Cells(i, 1) = "End Sub" Then Exit For
    If sh.Cells(i, 1) = "End Function" Then Exit For
    ctr = ctr + 1
    fi = Format(ctr, "000")
    sh.Cells(i, 1) = fi & "  " & sh.Cells(i, 1)
Next_i:
Next i


sh.Cells(7, 1).Activate
   
End Sub
 
Private Sub RemoveLineNumbers_Click()
'cooment
Set sh = Sheet4


For i = 7 To 1000
    If sh.Cells(i, 1) = "" Then GoTo Next_i ' find start of code
    If sh.Cells(i, 1) = "End Sub" Then Exit For
    If sh.Cells(i, 1) = "End Function" Then Exit For
    sh.Cells(i, 1) = Mid(sh.Cells(i, 1), 5)
Next_i:
Next i
    
sh.Cells(7, 1).Activate
    
End Sub
 
Last edited by a moderator:
Upvote 0
Correct me If I am wrong. Isn't it what the requester is asking for? If yes, then the numbering of the code is possible in Excel. Please see below and suggest.

Code:
Sub test()
1   On Error Resume Next
2   i = 1 / 0
3   MsgBox "Hi, I am line: " & Erl
End Sub
 
Upvote 0
It has already been addressed (comprehensively) earlier in the thread that you can have line numbers but there is no built-in feature to add or remove them - you must do so yourself, or use a third-party add-in or code.
 
Upvote 0
Hello mikerickson,

What references should be enabled in order for this to work? While running it tells me that vbext_pk_Proc is not defined. If I remove the Option Exsplicit then Excel tells me: ByRef argument type mismatch and points back to vbext_pk_Proc. How should vbext_pk_Proc be decalre what type of variable is it, string?

:confused:

Thanks.

Microsoft Visual Basic for Applications Extensibility 5.3

worked for me
 
Upvote 0

Forum statistics

Threads
1,223,678
Messages
6,173,803
Members
452,535
Latest member
berdex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top