How to comment code out efficiently

If you want to have certain lines of code not run, but you don't want to delete them, you can comment them. Another reason to comment is that you want to add explanatory text.

In VBA commenting code is done by putting a ' (quote) either at the beginning of a line or at the point of the line where the code should be commented. The VBA editor VBA colors the line green to indicate that it's a comment

'TODO: test with begin and end date
'datPicked = PickDate(BeginDate:=#4/15/2015#)
datPicked = PickDate(BeginDate:=#4/15/2011#, EndDate:=#4/15/2015#) '#4/15/2016#

Commenting a code block

comment lines out in complete procedure

Often commenting needs be applied on a block of code. This can be a part of a (or whole) procedure that you are rewriting but you want to keep your previous version of the code - just in case. This may involve commenting quite a few lines. Spending time on putting a ' on front of each line is a bit a waste of time. In Code VBA there are two tools that can make your life easier when commenting lines which are described in the next paragraphs.

Comment a whole procedure

Use the Code Explorer to comment a whole procedure - as in image.

comment menu

Comment a selected block of code

Use the Code VBA menu to comment a block of code

  1. Select the lines to comment
  2. On the Toolbar click Toolbar comment lines or the Code VBA menu select Comment » Comment Lines (Alt-CMC)

If you selected commented code, the menu lets you uncomment previously commented code.