How to Utilize Comments on VBA
-
Adding
'
on Each Line to Comment in VBA -
Utilizing the
Comment Block
Button on VBA -
Utilizing the
Uncomment Block
Button 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.
- Manually adding
'
at the start of the comment line - Utilizing the
Comment Block
button on VBA - 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:
- Go to
View
thenToolbars
- Select
Edit
. A new toolbar withComment Block
andUncomment 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