How to Utilize Comments on VBA

Glen Alfaro Feb 02, 2024
  1. Adding ' on Each Line to Comment in VBA
  2. Utilizing the Comment Block Button on VBA
  3. Utilizing the Uncomment Block Button on VBA
How to Utilize Comments on VBA

Comments are human-readable text describing the code’s logic and algorithm. Adding comments on a code block, you are writing/debugging is a good coding practice.

It will help you and future developers easily understand the code’s logic. Prolific programmers are distinguished by structuring their scripts and putting descriptive comments on each code block.

This article will demonstrate how to create comments on VBA effectively.

In VBA, we only use single-line comments or comments that are only effective on a single line of code.

An apostrophe (') is used to dictate the start of a comment line.

Three methods of dealing with comments in VBA are below.

  1. Manually adding ' at the start of the comment line
  2. Utilizing the Comment Block button on VBA
  3. Utilizing the Uncomment Block button on VBA

Adding ' on Each Line to Comment in VBA

In this method, you can create a comment by manually typing an Apostrophe ' at the beginning of the line you want to be a comment.

Example:

Sub test()
'This is a comment.
End Sub

You may add a line-continuation character (_) at the end of the line if you want to extend the comment to the next line.

Example:

Sub test()
'This is a comment. _
This is still a comment.
End Sub

Utilizing the Comment Block Button on VBA

In this method, we need to highlight the code we want to convert into comments then click the Comment Block button in the toolbar.

If the Comment Block button is not in the toolbar, you may need to do the following steps:

  1. Go to View then Toolbars
  2. Select Edit. A new toolbar with Comment Block and Uncomment Block buttons will appear.

Example:

Sub test()
This text should be highlighted, then click Comment Block Button to be converted to comments.
End Sub

Output:

Sub test()
'This text should be highlighted, then click Comment Block Button to be converted to comments.
End Sub

Utilizing the Uncomment Block Button on VBA

Example:

Sub test()
'This text should be highlighted, then click Uncomment Block Button to be converted to comments.
End Sub

Output:

Sub test()
This text should be highlighted, then click Uncomment Block Button to be converted to comments.
End Sub