How to comment a line in VBA?
In VBA, you can comment a line of code by adding
- an apostrophe (single quote – ‘) or
- statement ‘REM’
at the beginning of the line. This tells VBA to ignore the line & not execute the code. Here is an how the code looks with few commented lines.
Sub Commenting_a_Line_in_VBA() 'Here is how a commented line looks like in VBA 'Almost everyone uses single quote to comment 'MsgBox "commented line" 'Or use keyword REM Rem MsgBox "commented line" 'Uncommented Code MsgBox "commented line" End Sub
Programmers add comment as, it is a good practice to use comments to explain what the code is doing. This makes it easier for others (or yourself) to understand what the code does.
How to Comment Multiple Lines in VBA Code?
In Excel VBA comment multiple lines of code with this built in option available in toolbar menu:
Follow these steps to enable VBA comment multiple lines of code.
- In VBA Menu click View
- Choose Toolbars -> Edit
- New Icon toolbar will be displayed
- Click on highlighted icons to comment or uncomment multiple lines of code.
Check all the options in the newly displayed toolbar. One of the icon will display tooltip ‘Comment Block’.
In most programming languages, this can be done with a keyboard shortcut. But, there is no keyboard short available for commenting single line of multiple lines in VBA.
Even then, VBA editor has built in menu option to comment multiple number of lines of vba code.
Steps to Comment Multiple lines in VBA
Once the above Toolbar is enabled, You can drag it to quick access space just below the menu. It will always stick there when you open VBA editor next time.
Now, you can follow these easy steps to comment any number of selected block of code in your current VBA Project.
- Select multiple lines of code.
- Click on the option ‘Comment Block’ from Edit Toolbar.
- All the selected lines will be commented.
- Click on the icon adjacent to “Comment Block”
- Now the comments will be removed.
Similar to commenting, the next option in the toolbar is uncommenting.
This is the reverse of the above option used to uncomment the already commented block of code. This is how You can add comments or comment multiple lines in VBA using simple toolbar option. Hope you like this option. Lets us know what you think of this article in the comments section.
Related Topic that might interest you.