...

MS Excel VBA

Utilizing Excel VBA comments (Add, Remove, Block Commenting)

Utilizing Excel VBA comments (Add, Remove, Block Commenting)

You may quickly add comments as you type the code while using VBA in Excel.

Beginners might find comments in VBA to be very helpful because they can add a comment to a line of code (or a block of code) that describes what it does. The comments will give you some context so that when you return to the code in the future, you won’t be completely lost.

Once the code reaches more than a few lines, it is a good idea to add context using comments, even for experienced Excel VBA programmers.

Furthermore, because it is a comment, VBA ignores it when running the code.

I’ll go through how to add comments in VBA and all the recommended practices associated with it in this brief Excel lesson.

This instruction explains: 1. Adding Comments in Excel Using VBA 2. Creating a Comment from a Line of Code (or a Block of Code) 3. How to Change the Comment's Color in Excel VBA 4. Some Recommended Techniques for Using Comments in VBA

1. Adding Comments in Excel Using VBA

Simply use an apostrophe before the line you wish to indicate as a comment to add one in VBA.

Anything in that line that comes after the apostrophe is seen as a comment, and VBA will color it green (to visually differentiate it from regular code)

Comment in VBA Example

In VBA, you may comment in one of two ways:

Put each remark on its line, with the comment text after the apostrophe at the beginning of the line.

Comment within the code in a separate line

Include a comment in the same line as the regular code, separated from it by a space character, an apostrophe, and the comment.

Comment in the same line in the code

Although I’ve seen VBA programmers utilize both of these, I favor the first approach where a remark gets its line entirely.

Another (traditional) way to add a comment is to use the word “Rem” after the statement.

REM comment in Excel VBA

The remark is abbreviated here as Rem.

Rem was utilized back when BASIC was popular and is still used in the most recent iterations of VBA. Even though it exists, I advise against using it unless you’re writing comments in VBA.

2. Creating a Comment from a Line of Code (or a Block of Code)

You might occasionally need to change a line of existing code (or a block of existing code) into comments.

This is something that programmers frequently do when they are writing code and want to rapidly try out something different while maintaining the existing code.

As a result, you may rapidly comment out a line, and test a new one, and if you want to get the previous code back, just delete the apostrophe and turn the comment back into a regular line of code.

It’s best to manually insert the apostrophe before these lines for a line (or even a few lines).

However, if your code is lengthy, follow the instructions below to give yourself the option of turning the entire block of code into a comment:

Toggle to the View tab.

Click the View option

Select Toolbar from the menu.

More choices appear when your cursor is over it.

Choose Edit from the menu. The edit toolbar will then show up somewhere on your screen as a result.

Click on Edit

If the Edit toolbars aren’t already docked, drag them over to the toolbar area to make them appear there.

Choose the section of code you wish to comment out.

Select “Comment Block” from the toolbar by clicking on it.

Comment block icon in the toolbar

By placing an apostrophe in front of each line of the code, the aforementioned procedures would instantaneously turn a block of code into comments.

Select that section of code once more, then click the “Uncomment block” button in the Edit toolbar to remove the comment and return it to ordinary code lines.

3. How to Change the Comment’s Color in Excel VBA

Even though VB doesn’t have a lot of formatting options, you may choose to modify the comment’s color.

One of my VBA course participants wrote me and said how handy it was for persons with color blindness to be able to modify the color of comments in VBA.

The Excel VBA steps for changing the comment’s color are listed below:

  1. Activate the Visual Basic Editor
  2. Select Tools from the drop-down menu.Click the tools option in the menu
  3. Select Options.Click on Options
  4. Click the “Editor Format” tab in the Options dialogue box.Select Editor Format tab
  5. Choose Comment Text under Code colors.Select Comment Text option in the left pane
  6. Alter the background or foreground colorChange the color
  7. dialogue box closed

All of the current comments in your code would likewise change color if you changed the color of one comment.

4. Some Recommended Techniques for Using Comments in VBA

The following are some best practices to follow when utilizing comments in VBA code.

  1. Maintain the comment’s meaning and provide context. Think about what might be beneficial to a new user who has never seen this code before and is attempting to understand it when you add a remark.
  2. Keep your comments to a minimum since too many will make your code appear cluttered. While it’s OK to add more comments while you’re just starting, as you get more skilled at writing VBA code, you won’t necessarily need to do so.
  3. It’s a good idea to provide a note that describes what each new subroutine or function accomplishes.
  4. It’s a good idea to add comments before conditions and loops when working with complex code so that you can more easily understand what you did when you return to the code.

I went over adding comments in VBA and some best practices for doing so in this tutorial.

I sincerely hope this tutorial was helpful.

 

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top
Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.