Excel VBA Block Comment
When we write codes in VBA sometimes these codes are meant for other people or other developers. Just a single glance will not provide immediate attention on which function is used for what. This is not the only case for VBA be it any programming language. Commenting on a block of codes which is a fundamental feature of any programming language. Well in VBA, comment block is not enabled by default, we have to do it ourselves.
Now we know what a block comment means, in this article, we will learn about various ways of commenting on any block of codes in VBA. Comments in VBA will not be executed or compiled with the other codes. Comments are only there to help any other user or oneself while debugging to remember which code is for what. For example, if I write a function to add two numbers I can write a comment “Addition of two numbers” which will help me in the near future when I need to reevaluate the code or any other programmer looks at my code. It will also help him understand what a certain function or a block of code does.
Now there are three options in VBA to put comments in blocks of codes. We will learn each option by examples. But first, let me explain what these options are and how they work.
- The first option is the very simple one, we write any statement in a single quote (‘) it automatically becomes a comment in VBA.
- The second option is an inbuilt feature in VBA which needs to be activated which can comment or uncomment a block of words.
- The third option is also a very easy option, it is to use the REM keyword. REM keyword changes any statement into a comment in VBA.
Now I have explained the methods to block comments in VBA now let us use them in examples and try them.
But first, ensure that you have a developer’s tab enabled in order to use VBA. Developer’s tab can be enabled from the files tab in the options section.
Comment a Block of Code in Excel VBA
Below are the three processes to comment a block of code in Excel VBA and they are as follows:
- Using a single quote (‘)
- Using an inbuilt feature in the View tab
- Using Rem keyword
VBA Block Comment – Example #1
Now let use the first method which is a very simple one using the single quote.
Step 1: As we discussed above in order to enter VBA we need to go to the Developer tab and click on Visual Basic to open VBA Editor.
Step 2: Now once we are in VBA we need to insert a module. Module in VBA is where we write our blocks of codes.
Step 3: Once we double click on the module we can see the code window. Start by declaring the name of the macro to start the code.
Code:
Sub Sample() End Sub
Step 4: Let us write a sample code then we will insert comments in it. Have a look at below the sample code I wrote.
Code:
Sub Sample() Worksheets("Sheet1").Activate Range("A1").Value = 20 Range("B1").Value = 30 Range("C1").Value = Range("A1").Value + Range("B1").Value End Sub
Step 5: Now after each line of code use single quote and write comments like the below screenshot.
Code:
Sub Sample() Worksheets("Sheet1").Activate 'To activate worksheet Range("A1").Value = 20 Range("B1").Value = 30 Range("C1").Value = Range("A1").Value + Range("B1").Value End Sub
Step 6: We can see that the comment we wrote is in green color and we do not need to end it in a single quote. Now we can insert multiple comments as follows.
Code:
Sub Sample() Worksheets("Sheet1").Activate 'To activate worksheet Range("A1").Value = 20 'Value of A1 Range("B1").Value = 30 'Value of B1 Range("C1").Value = Range("A1").Value + Range("B1").Value 'Addition of A1 and B1 End Sub
Step 7: We can run the above code and see that only the block of code gets executed not the comments. Press F5 to see the result.
This was the simplest way to insert comments in VBA.
VBA Block Comment – Example #2
Now we will use the second option which is to comment and uncomment from the inbuilt feature from View bar in VBA.
Step 1: As we discussed above in order to enter VBA we need to click on the Developer’s tab.
Step 2: Now we have already inserted our module so we do not need to insert a new module. We can work on the same module.
Step 3: Start by naming the macro so that we can write the code.
Code:
Sub Sample1() End Sub
Step 4: Now write a sample code as below and then we will comment on it.
Code:
Sub Sample1() Dim A, B, C As Integer A = 10 B = 20 C = A + B MsgBox C End Sub
Step 5: Now write the comments on the codes as follows.
Code:
Sub Sample1() Dim A, B, C As Integer 'Declaring three variables A = 10 B = 20 C = A + B 'Adding them MsgBox C End Sub
Step 6: Now go to the View tab and then to Toolbars as follows,
Step 7: In the toolbars section we can find the option for Edit as follows, Check the option for Edit.
Step 8: When we click on it we get a rectangular wizard box as follows which as an option to make a line as a comment,
Step 9: Select the code which we want to make it as a comment and then click on comment block as follows,
Step 10: Now run the code by pressing the F5 Key and we can see that the code is not executed.
We will get a blank result as the line which had values and declared the variables is not termed as comments. In order to make the code to run we need to uncomment the statements.
VBA Block Comment – Example #3
Now we will use the third option which is the REM keyword.
Step 1: As we discussed above in order to enter VBA we need to click on the Developer’s tab.
Step 2: Now we have already inserted our module so we do not need to insert a new module. We can work on the same module.
Step 3: Start by naming the macro so that we can write the code.
Code:
Sub Sample2() End Sub
Step 4: Write a sample code as follows.
Code:
Sub Sample2() Dim A, B, C As Integer A = 10 B = 20 C = A + B MsgBox C End Sub
Step 5: Now use REM keyword to insert comments as follows.
Code:
Sub Sample2() Dim A, B, C As Integer Rem declaring variables A = 10 B = 20 C = A + B Rem adding them MsgBox C End Sub
Step 6: Now press F5 to run the code and see the result.
We can see that the comments were not executed but the codes were.
Things to Remember
- Starting with single quote adds the comment and it doesn’t mean we have to end it with a single quote.
- Comments are not executed in VBA.
- Codes which are turned into comments are also not executed.
Recommended Articles
This is a guide to VBA Block Comments. Here we discuss three processes to comment a block of code in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –