I want to have line numbers in my VBA code for debugging reasons. That will allow me to know where a particular error occurred.
Is there an automatic feature for this (such as an option in settings)? Or do I need to write my own macro?
If I need to write my own macro to accomplish this task, how would I go about doing such a thing?
This Works for me...Add this to its own module. Calling the code will toggle line numbers on or off. Adding Module titles and/or procedure titles in quotes will update only the module or procedure named.
This is not 100% tested, but using VBA extensibility you could do the following
This gives the results before and after as below, altering in this way is not recommended though.
You don't want line numbers.
Not for "debugging reasons", not for anything. Line numbers are deprecated for a reason: they're a relic of an ancient time before procedures even existed, and
GOTO
the only way to get anywhere.Erl
only returns the last encountered line number before an error was raised. This can mean misleading error logs, if you're logging errors:Also, line numbers only have
Integer
resolution (a VBA module can have up to 65,535 lines, which is twice that resolution), and will silently fail and happily report wrong line numbers if you don't keep that in mind:Any serious VBA application will use structured error handling instead.
Write small, specialized procedures (i.e. write code that follows modern-day best practices), and the line number becomes utterly meaningless.
Line numbers are a pain in the neck to maintain; they clutter up the code and make it overall harder to read (and therefore to debug).
That said, IIRC MZ-Tools 3 had such a functionality.
Keep in mind, that BASIC looked like this when line numbers were a thing:
The above is a working Commodore 64 BASIC 2.0 fizzbuzz program. VBA has fabulous backward-compatibility. With only slight modifications, it runs in VBA:
Don't write 1980's code, we're 40 years later.
The VBA editor has a built in way to see a line number, under the 'Standard' toolbar:
When you select a line of code, the line number will be represented here next to 'Ln'.
I use this code for adding line numbers to my Excel projects. I found it online a while back and I don't remember where I got it, so credit goes to whoever originally wrote this:
You'll have to modify it to suit your needs since you're working in Access, but I'm sure the main meat of it still applies. In Excel, there's a userform that is used to kick off the code for the module you specify, but you should be able to just pass in the module name (
vbCompName
) to specify the module. I'm not well-versed in Access VBA, so I'm not sure what you'd replaceWorkbooks(wbName)
with in the code.The answer of Arich works like a charm on an individual module. If you want to provide your entire workbook with (updated) line numbers you apply the following steps*^:
Do Once:
Module2
in your workbook.Module3
in your workbook.Module4
in your workbook.Global allow_for_line_addition As String
this is just so that you can automatically add linenumbers` above/in the first line of every module.end sub
,end function
orEnd Property
of a module).Do every time you have modified your code:
Module3
to remove line numbers to all the modules in your workbook.Module4
to add line numbers to all the modules in your workbook.Module2
:Module3
:Module4
:where you can either substitute
"Book1.xlsm"
with the name of your own workbook, or withthisworkbook
(notice no ""), or vice versa.line 2440:
aboveline 2303:
). By removing and re-adding them, the line numbering is automatically correct again.