Commenting out code in VBA
Part three of a three-part series of blogs

Commenting code is an art form! This series of blogs explains why, how and when to comment in VBA.

  1. Why Write Comments in Excel Visual Basic
  2. Adding Comments in VBA macros for Excel
  3. Commenting Out Code in Excel Visual Basic (this blog)

This blog is part of our Excel macros online tutorial series.  Alternatively, come on one of our VBA classroom training courses.

Posted by Andy Brown on 28 April 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Commenting Out Code

Rather than have to type an apostrophe before every line of code, you can select a block of code and comment it out:

Selecting code to comment out Commented out code
Select the code to comment out ... ... to tell VBA to ignore it

To do this, you first need to display the correct toolbar.

Displaying the Correct Toolbar 

The easiest way by far to comment out code is using the Edit toolbar: 

The Edit toolbar

Right-click on any toolbar in VBA and tick the Edit toolbar to display it

Once you have this visible,  you can comment code out as shown below.

Commenting Out (and Back In) 

To comment out code, first select part or all of the lines of code you want to comment out: 

Selected code for commenting out

You don't have to select all of each line (here we'll comment out the 3 lines of code wholly or partially selected).

 

You can then comment the lines out: 

Comment out tool

Click on the tool shown to comment out selected lines (or the one to its right to comment them back in)

 

Commenting out code is particularly useful for lines copied from the Internet.

  1. Why Write Comments in Excel Visual Basic
  2. Adding Comments in VBA macros for Excel
  3. Commenting Out Code in Excel Visual Basic (this blog)
This blog has 0 threads Add post