How to Convert a Block of Code into a Comment Using Excel VBA (4 Methods)

Comments play a crucial role in code by guiding readers to understand the context, functionality, and purpose of the written code. In this article, we’ll explore various methods to convert a block of code into a comment within the Excel VBA Editor. Let’s get started!

Excel vba comment block


Why Use Comments in a Code?

Comments serve several purposes in code:

  1. Explanation: They explain what the code is doing and provide insight to the reader.
  2. Context: Comments help other programmers (and your future self) understand the code’s purpose and functionality.
  3. Readability: They break down complex or lengthy sections of code into smaller, more understandable chunks.
  4. 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.

Commenting Block of VBA Code Using Apostrophe in Excel


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.

Commenting Block of VBA Code Using Rem in Excel

Note that the Rem keyword cannot be used in the middle of a line to convert the rest of the line into a comment.

Rem Can not be used in the Middle of a Line


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.

Comment Block and Uncomment Block Buttons on ToolBar

However, these buttons may not be readily available by default. If you don’t see them, follow these steps:

Steps:

  • Go to View > Toolbar and check the Edit option.

Checking Edit in Toolbar

  • 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.

Commenting Block using Command Button in Excel VBA

  • To uncomment, select the lines and click the Uncomment Block button.

Uncommenting Bloc using Command 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 > Toolbars > Customize…

Opening Custom Toolbar

  • In the Commands tab, select the Edit category.
  • Find the Comment Block option and drag it to the end of the toolbar.

Dragging The Comment Button on the Toolbar

  • Right-click on the icon and rename it as “&Comment Block.” Check the Image & Text option for clarity.

Renaming Command Button

  • Similarly, add the UnComment Block option and rename it as “&Uncomment Block.”

Renaming UnCommand Button

  • Now you can use Alt+C to comment and Alt+U to uncomment any block of code.

Commenting Using Alt+C

Uncommenting Using Alt+U


How to Uncomment a Block of Code in Excel VBA Editor

Removing Apostrophe-Based Comments

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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!

Tags:

Aniruddah Alam
Aniruddah Alam

Md. Aniruddah Alam, a graduate of Bangladesh University of Engineering & Technology, Dhaka, holds a BSc.Engg in Naval Architecture & Marine Engineering. Joining Softeko in August 2022 as an Excel and VBA content developer, he has authored 90+ articles, covering fundamental to advanced Excel topics. He actively develops VBA codes for Excel automation and offers solutions in the Exceldemy Forum. Beyond work, he finds leisure in reading books, and taking relaxing walks, showcasing well-rounded blend of professional and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo