3 Methods to Comment or Uncomment in Excel VBA

.

Dear Friends,

This is a very simple yet useful article, especially for beginners. Sometimes even intermediate-level users also do not know about all these options to comment on a line or group of lines in Excel VBA.

Like every other programming language, VBA too has an option to comment on a line or group of lines in your code.
For those who does not know – what is comment in a Programming:

Commenting a line or group of lines in any programming language means – to instruct the execution control or compiler to skip those lines from compilation or execution.

Before I tell you how to comment on a line in Excel VBA, let me explain to you – the use of it

What is the use of comments in Excel VBA Programming

As mentioned above these comments is something that is not compiled or executed, a programmer can write anything in any simple human language to give a brief description of a function, a variable, a statement, etc.
This makes the programmer’s life easier from a maintenance perspective. By reading those brief descriptions of the code, any programmer can have a basic idea of what that function or statement or variable, etc. does.

It is not MANDATORY but a good programming practice to provide proper comments so that any other programmer can easily understand the code easily.

How to comment a single line in VBA code

Basically, there are 3 ways to do so.

Method 1: Using Single Quote (‘)

This is one of the simplest methods to comment on a single line in VBA programming.
To comment on a line, you can simply type a single quote (‘) at the beginning of that line. The whole line will turn into green text. That’s all!
Now that line would neither be compiled nor executed.

To Comment a line in VBA

To Comment a line in VBA

Note:

A single quote can be put somewhere in the middle of a line as well. In such case, rest of the text after the single quote (‘) will be treated as comment.
Refer to the below image

Comment Line in VBA

Comment Line in VBA

Method 2: Using REM Keyword in Excel VBA

This is also a simple one but I do not prefer this. You can see the reason below in the note section.

To comment on any line in VBA you can start that line with the keyword REM So in this method, instead of a single quote (apostrophe) you can type the Rem keyword.

Rem Keyword to comment a line

Rem Keyword to comment a line

Note:

Following are the limitation using this Rem keyword
1. There should be at least one space between the start of your comment and the Rem keyword.
2. Like a single quote, the Rem keyword can not be used in the middle of a line to comment rest of the line. Rem keyword must always be the first word to start with.

I think the above two reasons are more than enough for me not to like it :).

Method 3: VBE built-in Option – Comment/Uncomment Block

This is the same method as the first one, but here you do not need to type a Single quote by yourself, rather you can follow the below steps:
1. Place your mouse cursor anywhere on the line on which you want to comment
2. Press on “Comment” button as shown in below video

Toggle between comment and un-comment

Toggle between comment and un-comment

Important:

Using this method you also get an option to press the “Uncomment” button as well as shown in the above gif video.

How to comment or un-comment multiple lines together

Unfortunately in VBA, there is no shortcut key to do so. But don’t worry, method 3 is very useful in this case.
You can use the same buttons Comment Block and Un-comment Block to do this.
Simply select all the lines you want to comment on or uncomment it and press the relevant button as shown below:

1# How to comment group of lines (Block) in VBA

Step 1: Select all the lines (block)
Step 2: Press the Comment Block as shown in above picture

Comment Uncomment VBA Methods

Comment Uncomment VBA Methods

2# How to Un-comment group of lines (Block) in VBA

Step 1: Select all the commented lines (block)
Step 2: Press the Uncomment Block as shown in above picture

Comment Uncomment VBA Methods

Comment Uncomment VBA Methods

What if Comment Block and Uncomment Block Button not visible

Follow the below steps:
Step 1: Go to View –> Toolbars –> Edit

VBE - View Edit Option

VBE – View Edit Option


Step 2: Check the Edit option. You will see the Edit toolbar where you can find these two options along with others.

Comment-uncomment-button

Comment-uncomment-button

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Convert PDF Tables to Excel with VBA | Step-by-Step Guide
Convert PDF Tables to Excel with VBA | Step-by-Step Guide

Convert PDF Tables to Excel with VBA | Step-by-Step Guide How to Convert PDF Tables to Excel Using Excel VBA (Macros) Extracting data from PDF tables into Excel can be tedious, but with Excel VBA (macros), you can automate this process and save time. This guide will...

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

3 Comments

  1. Benigna Berardi

    In Office 2010, this also works if you choose Text Only (Always) in Step 7, rather than Image and Text. It s obnoxious that it doesn t work if you choose any other display option, but at least on my screen I prefer to save a little space with Text Only. Air Jun 6 ’16 at 18:30 There is a built-in Edit toolbar in the VBA editor that has the Comment Block and Uncomment Block buttons by default, and other useful tools.

    Reply
  2. josja

    Private Sub CommandButton1_Click()
    Dim Inter(30)
    SiteEn = Cells(2, 4).Value
    DeviceIP = Cells(3, 4).Value
    HP1 = Cells(4, 4).Value
    HP2 = Cells(5, 4).Value
    p1 = “show ip int br”
    p2 = “show int des ”
    p3 = “show run | ” & HP1
    p4 = “show run | ” & HP2

    i = 7
    k = 0
    j = 1
    Do While Not j = 300
    Cells(j, 7) = “”
    j = j + 1
    Loop

    Do While Not Cells(i, 4) = “”
    Inter(k) = Cells(i, 4).Value
    k = k + 1
    i = i + 1
    Loop

    Cells(2, 7).Value = “Site Entity : ” & SiteEn
    Cells(3, 7).Value = “Device IP : ” & DeviceIP
    Cells(5, 7).Value = ” =============Precheks============ ”
    Cells(6, 7).Value = p1
    Cells(7, 7).Value = p2
    Cells(8, 7).Value = p3
    Cells(9, 7).Value = p4

    Cells(11, 7).Value = “conf t”
    i = 12
    n = 0
    Do While Not k = 0

    Cells(i, 7).Value = “interface ” & Inter(n)
    i = i + 1
    Cells(i, 7).Value = “no ip-helper ” & HP1
    i = i + 1
    Cells(i, 7).Value = “no ip-helper ” & HP2

    i = i + 2
    n = n + 1
    k = k – 1
    Loop

    i = i + 1
    Cells(i, 7).Value = “end ”
    i = i + 1
    Cells(i, 7).Value = “write”

    i = i + 2
    Cells(i, 7).Value = ” =============Postchecks============ ”
    i = i + 1
    Cells(i, 7).Value = p1
    i = i + 1
    Cells(i, 7).Value = p2
    i = i + 1

    Cells(i, 7).Value = p3
    i = i + 1
    Cells(i, 7).Value = p4

    End Sub

    Reply

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest