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.
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
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.
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
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
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
What if Comment Block and Uncomment Block Button not visible
Follow the below steps:
Step 1: Go to View –> Toolbars –> Edit
Step 2: Check the Edit option. You will see the Edit toolbar where you can find these two options along with others.
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.
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
soul delta 8 og kush