Pages

Comment Block of Code in VBA

Did you know that you can comment out block of code in VBA? Yes you can. But VBA’s method is little different from other programming languages. Because other languages like Java, PHP etc. have specific syntax to comment a block of code. But in VBA you can comment and uncomment blocks of codes using two buttons. VBA doesn’t have a dedicated syntax to comment a block of code.

In VBA you can comment a single line of code using the ' (apostrophe mark).

'This is a comment

Then if you want to comment out multiple lines, you have to add apostrophe marks in front of all of those lines.

'This is a comment line 1
'This is a comment line 2
'This is a comment line 3

But other programming languages have a dedicated syntax to comment multiple lines. For example, this is how you can comment a block of code in Java.

java syntax of commenting a single line and a block of code

So if you need to comment multiple lines in your VBA program, adding apostrophes manually is not easy. Luckily as I said above Visual Basic Editor has two buttons to comment and uncomment the lines of codes. Here is how you can do it.

First check whether your Visual Basic Editor already has “Edit” tools in the Toolbar.

Check whether Edit tools are available in the Toolbar

If they are available in the editor then click here to jump to the next step. If they are not available, follow these steps to add them to the Toolbar.

Click on the “View” menu.

Click on the View menu

Take the mouse pointer on top of the “Toolbars”. Then a list of names of toolbars will appear to the right. Put a tick in front of the “Edit”.

Put tick to the Edit toolbar

Then “Edit” tools will be available in the VBA editor.

Edit tools

Once “Edit” tools are available, you can use these two buttons to comment and uncomment a block of codes.

Comment and Uncomment buttons

Now let’s look at how to comment and uncomment a block using these two buttons. First select the section you want to comment out.

Select the section you want to comment out

Then click the “Comment Block” button.

The section commented out by Block Comment button

Similarly if you want to uncomment a section, first select that section and then click on the “Uncomment Block” button.