Comment A Block In Vba In Excel


Dear Friends,

Hello All, I'm having a problem with making multiple lines of code a comment, instead of having to go line-by-line-by-line with an asterisk to turn into a comment. My reasoning is i want my macro to skip lines so I can test another set of code. I do not want to copy/delete to a clip board and then paste back in. In Code VBA there are two tools that can make your life easier when commenting lines which are described in the next paragraphs. Comment a whole procedure. Use the Code Explorer to comment a whole procedure - as in image. Comment a selected block of code. Use the Code VBA menu to comment a block of code. Select the lines to comment.

Comment A Block In Vba In Excel Cell

Providing proper short descriptions for each of your functions or statements etc. makes your VBA code – easy to read, easy to understand by anyone who refers your code.

Now the question is, how can you write description in English language in between your VBA code? VBA compiler will start giving compile error. It is very simple by commenting those lines which you do not want get executed.

How to comment a line in VBA code

In VBA to comment a particular line which you do not want to get compiled or executed, you can type your line followed by single quote ( ‘ ). The complete line which is starting with single quote is ignored and does not get executed. All the commented lines get formatted in green color as shown below image.

Commented line

Comment

It means to comment a block of code or lines you need to put single quote for each lines. It becomes difficult if you have got a function of hundreds of lines of code which you want to comment. then how to comment them all in simple way??

How to comment a block of codes or lines in VBA

In order to comment a block of lines at once, follow the below steps:

1. Select the Block which you want to comment
2. Click on comment Block option available as shown in below picture:

Commenting Block of codes in VBA

How to uncomment a block of codes or lines in VBA which are already commented

In order to un-comment a block of lines at once, follow the below steps:

1. Select the Block which you want to uncomment
2. Click on the un-comment Block option available as shown in below picture:

Where to find this comment Block Option in VBA window

Follow the below steps to enable or see this comment block option in VBA window:

1. To make this option visible follow : View –> Toolbars –> Click on Edit

Comment Block Option

Commenting on a code means inserting information inside the VBE that will not be executed (the information is used for guidance only).

This practice has some advantages:

  • It helps other users to understand the code, saving time on explanations
  • It helps to remember the purpose of a particular code
  • It prevents Excel from executing unwanted parts of the code when testing it

To comment on a code just use a simple apostrophe symbol. Everything that comes after the ' sign will be considered a comment:

We can also apply it to parts of the code to temporarily disable them:

When executing, the following text box should appear:

A comment may also be inserted into the same line after a code that will be executed.

After the apostrophe, the rest of the line will become comment.

Commenting on a Code Block

With the Edit Toolbar you can insert and remove comment blocks, just underline the code and use the buttons: Comment block and Uncomment block .

To add the Edit Toolbar in VBE go to: View $rightarrow$ Toolbars $rightarrow$ Edit.

Important Shortcuts in VBA Excel

Some shortcuts that will help save time:

  • +: Show all macros
  • +: Open VBE or return from VBE to Excel

Comment Block In Vba Excel

Shortcuts that will only work in the VBE environment:

  • +: Close the VBE and return to Excel
  • : Display help
  • : Object browser
  • : Properties window
  • : Run macro
  • : Display code
  • : Run a macro one step (line) at a time Important for learning and understanding
  • +: Project Explorer
  • +: Immediate window

FIRST STEPS

FUNDAMENTALS

RANGE

USERFORM

DEEPENING

SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.

Excel ® is a registered trademark of the Microsoft Corporation.

© 2021 SuperExcelVBA | ABOUT

Thank you for contributing. A message was sent reporting your comment.