Javatpoint Logo
Javatpoint Logo

Macro comments in VBA EXCEL

It was known that commenting on a single particular line in VBA (Virtual Basic for Applications) is a straightforward yet advantageous option from the respective programmers' viewpoint. Sometimes, we indulge in problems or need help to comment on a line, irrespective of how good we are at programming.

Moreover, the comments are used to pass instructions to the user about the code, function, or some statements in the non-specialist language. As it makes user's as well as the programmers' life too easy this is because the particular codes can be a good piece of information about what that code is about and how it can be used, or how some of the statements or the functions that are usually associated with one and another effectively.

Now let's move further and see the various methods used to comment on a single line and the group of lines, respectively.

How to Comment with the help of the VBA in Microsoft Excel?

Let us see the examples of Comments in Microsoft Excel VBA (Virtual Basic for Applications).

# Example 1: Commenting on the Single Line with the help of an Apostrophe or with the Single Quote (').

Any texts efficiently followed by an apostrophe sign are primarily termed as the Comment in Microsoft Excel VBA, and they will be opted out of the execution of the main code respectively.

Step 1: First, we will insert a new module in our Visual Basic Editor (VBE), as depicted in the screenshot below.

Macro comments in VBA EXCEL

Step 2: Now, we will define a sub-procedure in the newly inserted module to create a macro in VBE (Virtual Basic Editor).

Code in Virtual Basic for Application (VBA):


Macro comments in VBA EXCEL

Step 3: After that, we will be writing any piece of the text within the created macro. As we have noted, the line "This is a comment to be added".

Code in Virtual Basic for Application (VBA):


Macro comments in VBA EXCEL

Step 4: Before the first word of this Line, we will insert an apostrophe (Single quotation mark) to make this Line a comment effectively.

Code in Virtual Basic for Application (VBA):


Macro comments in VBA EXCEL

Step 5: As soon as we will insert an apostrophe before the first word of our Line, then it will become green, which means that the Line is commented out, as seen in the below-mentioned figure.

Code in Virtual Basic for Application (VBA):


Macro comments in VBA EXCEL

Step 6: Now, after that, we can add a Comment anywhere in the middle of our code with the help of an apostrophe, as seen in the below-attached figure.

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

And if in case, we can see this piece of code, under MsgBox, there is a text to be popped out in the message box, and then there is the Line of Comment, which says it is a line of Comment which needs to be added respectively.

Step 7: Next, we will be hitting the F5 or the Run button to run the code manually and then we will be able to see the output or the outcome as depicted in the below-attached figure.

Macro comments in VBA EXCEL

# Example 2: Commenting on the Single Line with the help of REM Keyword in VBA (Virtual Basic for Application)

This is considered one of the least suggested methods because it has pitfalls. Now let us see how it works:

Step 1:First, we need to insert a new Module in VBE (Virtual Basic Editor), as seen in the below-attached screenshot.

Macro comments in VBA EXCEL

Step 2: Then, we will define a sub-procedure to create a macro.

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Step 3:After that, we will add a line of text that we wanted to comment on out of the code. And then, we will take the Line "This is a comment line".

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Step 4:After that, we will use the keyword REM at the start of our Line, which will then be converted into a comment.

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Some of the pitfalls that are associated with REM are as follows:

  1. This keyword primarily doesn't allow us to add Comments anywhere in the middle of our code in any manner.
  2. And the other pitfall is that they always require at least a single space between REM and our Line of text for the proper evaluation of the Comment respectively.

# Example 3: Commenting out a Single Line

With the help of this respective method, we usually did not require to type apostrophes symbol at the beginning of our text; and then after that we need to click on the comment button which are available at the top of the VBA panel.

Step 1:First, we need to insert a new module and then define a new sub-procedure to create a macro, as seen in the screenshot below.

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Step 2: We will then add a comment line that we wanted to be skipped from our code execution.

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Step 3: After that, we will put our mouse's cursor anywhere on the Line we want to comment out.

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Step 4:Now, let's click on View, which is present under the uppermost panel of VBE (Virtual Basic Editor).

Macro comments in VBA EXCEL

Step 5: Then we will navigate towards theToolbarsand select theEdit options.

Macro comments in VBA EXCEL

Step 6:After that, we will be seeing a new toggle bar popping up in VBE as seen below, in which two buttons can be seen for the Comment and Uncomment purpose.

Macro comments in VBA EXCEL

Step 7:Now in these step we will be clicking on the Comment button and then we will see the Line of text that is being commented as seen in the figure below.

Macro comments in VBA EXCEL

These are the few methods by the help of which we can comment on a single line in VBA. And then, we will see how we can comment on a group of lines in VBA.

# Example 4: Commenting a Group of Lines in VBA Excel

It was assume, that there is no single keyword that could be used in order to comment on a group of lines in VBA (Virtual Basic for Editor). And we can use the particular apostrophe symbol to comment out each Line at a time.

Step 1:Let us assume that we have multiple lines of comments, as depicted below in the attached figure as well:

Code in Virtual Basic for Application:


Macro comments in VBA EXCEL

Now, in these three lines, we want to comment. As we have already seen in the edit toggle bar, it has Comment or Uncomment button.

Step 2: In these step we will be then selecting out all the lines which want to comment out and after that we will be clicking on the Comment Button which are present on the Edit toggle bar. As depicted in the below-attached figure.

Macro comments in VBA EXCEL

And now, this particular block of lines is being commented on. And we can make use of uncomment button in order to uncomment the particular range of lines.

Things to Remember

The important points or things that need to be remembered by any particular individual while working with the Macro Comments in Microsoft Excel are as follows:

  1. A single line that can be commented out with the help of the Apostrophe or the REM Keyword and placing them at the beginning of the Line effectively.
  2. And for a block of the text line, the Comment or the Uncomment button works handy to comment or uncomment a block of lines, respectively.
  3. The respective apostrophe symbol which can be effectively used anywhere in the mid of the code. So in this case, the particular Line next to the Apostrophe will then be considered to be a comment.
  4. Moreover, the REM cannot be efficiently used anywhere in the Line. And with the help of this keyword, then we can easily comment out the text at the beginning of a line.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA