In programming, a comment is a statement that is used to explain a line of code or a whole procedure. Comments are not executed or compiled.
They are also not mandatory, but it is a good practice to use them in your coding habits, especially if you are working in a team. Each programming language has his own way to make a comment, in this article, you are going to learn how to make a comment in VBA.
Why do we use comments?
Though comments are not mandatory, they can be very useful. Some of the reasons why we use comments are:
Provide a brief explanation about a function, a line of code or a macro.
Temporally disable a line of code or a macro if you do not want to delete and rewrite it again.
Better organize your code for easier readability, modifications, and teamwork.
How to make a comment in VBA
There are three main ways to can write a comment in VBA. Choosing the best method depends on the number of lines involved. In the following examples, we are going to look at each of them.
Comments using apostrophe (‘)
This method is mostly used when you want to comment a single line. To do that, first open the Visual Basic Editor (VBE) – follow the steps below if you don’t know how to open the VBE.
Step 1: Open a new Excel sheet and click on the developer tab or click on ALT + F11.
Step 2: Create a new module.
Step 3: Write any line of text beginning with the apostrophe.
You can put a comment on a separate line or at the end of a code line. All you need to do is to place the apostrophe at the beginning of the comment.
A comment is easily identifiable by the green font color.
Any code that is within the comment block will not be executed. That is why you can comment a code to disable it or uncomment it if you want it to be executed.
If your comment is on many lines, then you should begin each new line with the apostrophe.
Comments using the Rem keyword
This method is similar to the previous one using apostrophes. You just have to replace the apostrophe by Rem as in the screenshot below:
The Ren keyword works only for full line comments unlike apostrophe that works for both full line and inline comments.
There must be a space between Rem and the comment. It is not necessary with the apostrophe.
Comments using the editor toolbar
If you want to comment many lines at a time, it might be very cumbersome and time tacking to do one line at a time. This is where the Visual Basic Editor (VBE) built-in “Comment Block" feature is useful. This is how it works:
Step 1: Make sure the “Comment Block" icon is on the toolbar by doing the following:
Click on View (1) => Toolbar (2) => Customize (3) to open the “Customize" window as in the screenshot below.
Next, click on Commands => Edit then click on “Command Block" and drag it to the toolbar as shown in the screenshot below.
Step 2: Select the block that you want to comment (1) then click on the “Comment Block" icon (2) as on the screenshot below.
You can see from the screenshot below that the block selected has been commented:
Note that there is an apostrophe at the beginning of each line, meaning that the same result could have been obtained with the first method, but it would take you more time.
How to uncomment a commented line or block
As you might have guessed, to uncomment a commented line you just have to remove the apostrophe or Rem keyword from the line. For a block with many lines it might be very tedious to uncomment line after line.
VBE has a built-in “Uncomment Block" function to uncomment blocks of text just in one click. To achieve that, you need to place the “Uncomment Block" on the toolbar as you did previously with the “Comment Block".
Step 1: Open the “Customize" window as in the example above. Click on Commands => Edit then click on “Uncomment Block" and drag it to the toolbar as shown in the screenshot below:
Step 2: Select the block that you want to uncomment (1) then click on the “Uncomment Block" icon (2) as on the screenshot below.
As you can see from the screenshot below, the block selected has been uncommented.
In this article, you have been briefed on the importance of comments and how they can be created and removed. You have also learned that they are not mandatory but that you should develop the habit of writing them especially when you are working in a team or when you need to update your code regularly.