Comments we use in VBE to document the purpose of the procedures, functions, logic’s, and variables. It helps other programmers can effortlessly work on the same code in future. Comments are ignore by the interpreter and compiler and does not affect performance while execution. It is good programming practice or habit to begin all procedures or functions with a comment describing what it does. When you change any existing code details (statements), remember to update comment as well what it does.
You can also add comments to your code by preceding the text with the REM keyword. However, the Single Quote (‘) symbol and the Comment or Uncomment buttons are easier to use. It occupies less space in memory. Commented line always highlight in green color in the code window.
Note: While debugging lines or statements of code we can comment temporarily. The statements can be Uncommented afterwards. It is one of a useful technique to save processing time.
Here are the two methods to add comments to statements in Excel VBE.
Method 1: Any line or statement starts with a Single Quote (‘) is treated as comment in VBA.
Here is the simple example for your reference.
‘Here is simple example to comment code
You can also insert a comment at the end of the statement. Please find below example.
Dim iCnt As Integer ‘Here is another example to comment code
Method 2: Any line or statement that starts with the keyword ‘REM’ is treated as comment in VBE.
Here is the simple example for your reference.
REM Here is the simple example to comment code REM Date: 1st Jan 2018
The Comment Block and UnComment Block buttons on the Edit toolbar are very useful for commenting or uncommenting multiple lines of code in one shot.
Comment Block : Adds the single quote ( ‘ ) to the beginning of each line in the currently selected statements.
'Comment Line1 'Comment Line2 'Comment Line3 'Comment Line4
UnComment Block : Remove the single quote ( ‘ ) from the beginning of each line in the currently selected statements.
UnComment Line1 UnComment Line2 UnComment Line3 UnComment Line4
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers
I visit daily a few sites and blogs to read content, however this website gives quality based posts.
Thank you.