top of page
John

Commenting / uncommenting VBA code

There are various ways to comment / uncomment code in VBA:


  • Type out the Rem statement at the start of a line of code (or following a colon separating multiple statements on the same line) ... to uncomment, delete the Rem

  • Type an apostrophe at the start of a line of code ... to uncomment, delete the apostrophe

  • Use the VBE's (hidden ... ish) 'Comment Block' button ... to uncomment, use the VBE's (equally hidden ... ish) 'Uncomment Block' button

  • Use VBE_Extras 'Comment line(s)' command ... to uncomment, use the 'Uncomment line(s)' command


Which is best?


First, lets rule out using the Rem statement as it is an anachronism and IMHO shouldn't really be used. It's purpose is to maintain backward compatibility with earlier versions of the BASIC language.


All of the other options use the apostrophe to comment lines / blocks of code.


There's absolutely nothing wrong with typing an apostrophe at the start of a line of code if you just want to comment that one line and if your cursor happens to be at the start of that line. But ensure, if the 'logical' line has line continuations (in VBA, line continuations are achieved by using the underscore character which must be preceded by a space character ... a single 'logical' line can be made up of up to 25 'physical' lines) within it, that you add the apostrophe at the start of the first of the physical lines.


There's also nothing wrong with using the VBE's 'Comment Block' button (if you're not aware of this and want to use it, see the To show the VBE's 'Comment Block' and 'Uncomment block' buttons section, below). It's advantages over typing an apostrophe are that it will comment entire blocks (i.e. multiple lines) of code at once, also it will always add the apostrophe at the start of the line so you don't have to move the cursor to the start yourself - but note: if the line is made up of multiple 'physical' lines joined together by line continuations, the VBE is not 'smart' and will just add the apostrophe at the start of the physical line, potentially breaking your code (see the first of the two lines of code, below, in red ... text colour added by the VBE!).


Or you could use VBE_Extras 'Comment line(s)' command. It also will add the apostrophe at the start of the line so you don't have to move the cursor to the start yourself, however, if the line is made up of multiple 'physical' lines joined together by line continuations, it is 'smart' * and figures out the full extent of the 'logical' line and will add apostrophe(s) at the start of each physical line ... whether the line is continued before or after the position of your cursor (see the second of the two lines of code, below, in green ... text colour added by the VBE!). And just like the VBE's 'Comment Block' button, it will also comment entire blocks (i.e. multiple lines) of code at once.

Commented-out lines of VBA code

Additionally, while the VBE's 'Comment Block' and 'Uncomment Block' buttons can only be used via the Toolbar, VBE_Extras 'Comment line(s)' and 'Uncomment line(s)' commands can be used:

  • From the VBE_Extras Toolbar

  • From the main menu: 'Extras' > 'Lines' > 'Comment Block' / 'Uncomment Block'

  • From the code pane menu (right-click in the code pane then select: 'Extras' > 'Lines' > 'Comment Block' / 'Uncomment Block'

  • Via keyboard shortcuts - Alt+C for 'Comment Block' and Alt+Shift+C for 'Uncomment Block' (these, and all other VBE_Extras keyboard shortcuts, can be changed or disabled via the Settings dialog)


So to answer the question 'Which is best?' ... I just press Alt+C.


* The 'smart' behaviour of VBE_Extras 'Comment line(s)' command can be enabled / disabled in the 'Other' tab of the Settings.


Note that commenting line(s) of code that have Attributes will remove those Attributes ... this is the case no matter how you comment them and is because the VBE itself does not allow commented lines to have Attributes.


To show the VBE's 'Comment Block' and 'Uncomment Block' buttons


  • First ensure that the 'Edit' Toolbar is being shown: select: 'View' > 'Toolbars' and ensure a check is next to 'Edit'

  • Second, click the small 'Options' arrow/triangle at the far right end of the Toolbar (see image below), then 'Add or Remove Buttons' > 'Customize', under 'Commands' in the left-hand box select 'Edit' then scroll down in the right-hand box until you find 'Comment Block' and 'Uncomment Block' ... drag-and-drop each of them onto the Edit toolbar

The VBE's 'Edit' Toolbar



0 comments

Recent Posts

See All

Comments


bottom of page