Why Use Comments in a Code?
Comments serve several purposes in code:
- Explanation: They explain what the code is doing and provide insight to the reader.
- Context: Comments help other programmers (and your future self) understand the code’s purpose and functionality.
- Readability: They break down complex or lengthy sections of code into smaller, more understandable chunks.
- Troubleshooting: Comments can assist in locating issues during code error troubleshooting.
Method 1 – Using Apostrophes
The simplest way to comment in the VBA Editor is to add an apostrophe (‘) at the beginning of the line where you want to create a comment. When you do this, the Editor ignores that line during compilation. These commented lines are also colored green to indicate their status.
Method 2 – Use of Rem Keyword to Comment a Block
You can also use the Rem keyword to write comments. Rem stands for remarks. While it works similarly to the apostrophe, it provides more clarity and readability. Unlike the apostrophe, which can sometimes be misinterpreted as part of the code (e.g., a literal apostrophe in a string), the Rem keyword avoids such ambiguity. To use it, replace the apostrophe with Rem.
Note that the Rem keyword cannot be used in the middle of a line to convert the rest of the line into a comment.
Method 3 – Using Toolbar to Comment Block
If you find manually adding apostrophes to each line cumbersome, the VBA Editor includes built-in Comment and Uncomment command buttons on the toolbar. These buttons allow you to comment or uncomment multiple lines at once.
These buttons may not be readily available by default. If you don’t see them, follow these steps:
Steps:
- Go to View, select Toolbar and check the Edit option.
- The Comment Block and Uncomment Block buttons will appear on the toolbar.
- To comment a block of code, select the lines and click the Comment Block button.
- To uncomment, select the lines and click the Uncomment Block button.
Read More: Excel VBA to Comment Multiple Lines
Method 4 – Setting Up Shortcut Keys
You can create shortcut keys for commenting and uncommenting blocks of code. Let’s set up Alt+C for commenting and Alt+U for uncommenting:
Steps:
- Go to View, select Toolbars and click on Customize…
- In the Commands tab, select the Edit category.
- Find the Comment Block option and drag it to the end of the toolbar.
- Right-click on the icon and rename it as &Comment Block. Check the Image & Text option for clarity.
- Add the UnComment Block option and rename it as &Uncomment Block.
- You can now use Alt+C to comment and Alt+U to uncomment any block of code.
How to Uncomment a Block of Code in Excel VBA Editor
Removing Apostrophe-Based Comments
- Manual Removal: The quickest way to remove comments is to manually delete the existing apostrophes (‘) from each line where they appear. Simply edit the code and remove the leading apostrophe character.
- Using the Toolbar: The VBA Editor includes a built-in Uncomment Block command on the toolbar. If you’ve commented out multiple lines using apostrophes, select the lines and click this button to uncomment them all at once.
- Shortcut Key: Alternatively, you can use the shortcut key Alt+U to quickly uncomment a block of code. Select the lines and press this key combination.
Handling Rem Keyword Comments
- Manual Removal: If you’ve used the Rem keyword (short for “remarks”) to create comments, remove them manually by deleting the Rem from the beginning of each line. Unlike apostrophes, the Rem keyword provides more clarity and avoids ambiguity.
- Important Note: The Rem keyword only works at the beginning of a line. Attempting to insert it in the middle of a line will result in an error.
Frequently Asked Questions
What Are Comments Used for in VBA?
Comments serve as explanatory text within your code. They explain what the code does, provide insights, and help other programmers (and your future self) understand its purpose and functionality. Computers ignore comments during execution.
Can I Add Comments to Existing VBA Code?
Absolutely! You can insert comments into existing VBA code by placing text after an apostrophe (‘) or using the Rem keyword.
Are Comments Necessary in VBA Code?
While not mandatory, good commenting practices enhance code readability, maintenance, and debugging. Clear comments make your code more accessible to others.
How Should I Format VBA Comments?
There’s no strict formatting requirement, but concise language and placing comments before the relevant code are recommended. You can also use indentation or other formatting to distinguish comments from code.
Do Comments Affect VBA Code Performance?
No, comments have no impact on code execution speed. Computers ignore comments, so they won’t slow down your code.
Can I Comment Out a Block of Code in VBA?
Certainly! To temporarily disable a block of code, insert an apostrophe at the beginning of each line within the block. This is useful for debugging or selectively enabling/disabling code without deleting it.
Download the Practice Workbook
You can download the practice workbook from here:
Get FREE Advanced Excel Exercises with Solutions!