Excel Macro Comments is very useful to document the program logic which makes the other programmers can easily understand and seamlessly work on the same code in the future.
Comments in VBA include such types of information like developed by, modified by, and many more as the programmer like to write for documentation purpose. Comments are ignored by the interpreter when macros are executing.
Comments in VBA will always display as Green Text.
VBA commenting can be denoted by two methods.
#1. A statement which starts with a Single Quote (‘) is treated as comment.
#2. A statement which starts with the keyword REM is also treated as comment.
Excel Macro Comments type
- Single Line VBA Commenting
- Entire Block VBA Commenting
Excel Macro Comments in Single Line VBA
In Excel VBA, there are two ways to comment the code:
The simple way to comment a line of a code by putting a single quotation at the beginning of the line
' This code is written by Gunjan Kumar
'Sheet1.Range("A1").Value = "Gunjan Kumar"
Now we can notice that, comments are always displayed as green text.
As we can see in the example, by putting a single quotation at the beginning the whole line will be considered as commented line and will be skipped during execution of the code.
We can also comment part of the code if we put a single quotation somewhere in the line.
In this case code will skip this quotation. This is a best way for writing inline comments in VBA.
Sheet1.Range("A1").Value = "Gunjan Kumar" 'This is the example of Partial Commenting"
The second method for commenting a line in VBA by using the standard comment button from the toolbar. In order to display this button, first, we have to add it: View -> Toolbars -> Edit. Now you can see two buttons in the toolbar: Comment and Uncomment Block button (Available next to Comment Block button).
This can be done by selecting the line(s) of code and click on the Comment / Uncomment Button whichever needs to be used.
Note: Both buttons will comment/uncomment the entire line. it will not allow us to add comments at the end of a line.
We can also use the REM keyword to comment a line, only we have to write this keyword at the beginning of a line:
REM This code is written by Gunjan Kumar
Like Comment and Uncomment button, the REM keyword also allow us to comment complete line.
Excel Macro Comments for Entire Block
Instead of commenting a single line, we can also comment a block of code. In order to do this First, we need to select all the lines which we have to comment and then we will click on the Comment button.
Sub CommenttheEntireBlock() ' Sheet1.Range("A1").Value = "Gunjan Kumar" ' If Sheet1.Range("A1") = "Gunjan Kumar" Then ' MsgBox "Value in A1 cell is: Gunjan Kumar" ' End If End Sub
Now we can see the whole block of code is commented.
Also similarly, we can uncomment a block. first, we will select the lines of code by clicking on the Uncomment Block button in the edit toolbar.
Sub CommentEntireBlock() Sheet1.Range("A1").Value = "Gunjan Kumar" If Sheet1.Range("A1") = "Gunjan Kumar" Then MsgBox "Value in A1 cell is: Gunjan Kumar" End If End Sub