VBA Comments

A Comment is text written alongside code that is not executed when the code is run. Comments can be used to explain code or they can be used to disable code while leaving it in place.

Writing Comments

To create a comment in VBA, a single quote character is normally used. The Rem statement can also be used to create a comment. Comments can be written on their own lines or they can be written in-line after executable code. A Rem statement comment can only be used on its own line

Public Sub Example()

    Rem This is a comment using the Rem statement. Rem is short for Remark.

    'This is a comment on it's own line

    Debug.Print "Hello, World!" 'This is an in-line comment

End Sub

Commenting Out Code

To prevent a section of code from executing while leaving the code in place, "comment out" the code. Highlight a block of code and use the "Comment/Uncomment Block" tools on the Edit toolbar.

Edit Toolbar
Public Sub Example()

    Debug.Print "Hello, World!"

'    'This block of code will not be executed.
'
'    Dim i As Long
'
'    For i = 1 To 10
'        Debug.Print i
'    Next i

    Debug.Print "Done"

End Sub