Comments

A comment is simply some descriptive text that is included within your code.
This text is completely ignored when the program is compiled and does not slow your program down.
Comments should be used to describe what you are doing but probably more importantly why you are doing it.
Any text that follows an apostrophe is considered a comment.
Comments are displayed in green by default.


 


Guidelines for Comments

Always write comments as you code, not afterwards. Use comments to:

  • Describe briefly the purpose of each procedure you write.

  • Describe changes you make to a procedure at a later date.

  • Indicate that you are using a function or construct in an unusual or non typical way.

  • Describe the purpose of variables in their names and do not use cryptic names.

  • Describe workarounds that you develop to overcome any known bugs.

  • Comments should be prefixed with just one apostrophe character followed by the comment in lowercase. Only add comments that are useful.

  • Always put comments above the line of code

  • Do not use any unncessary formatting. A single apostrophe is all you need.

  • Add value added comments which explain why, do not add trivial comments.

  • Always check and ammend the comments when code is copied and pasted.


Why Should I include Comments ?

There are several reasons why you should include comments in your code:

  • The text inside any comments is completely ignored by the compiler and does not affect performance.

  • You should include comments when you develop any workarounds or have really complicated code.

  • You should obviously comment any changes that you make at a later date.

  • A useful debugging technique is to comment out lines of code temporarily. The lines can be easily uncommented afterwards.

Remember that your comments should describe the purpose of the procedures, functions and variables.


Include Comments

Any text that follows an apostrophe is considered to be a comment and is ignored up until the end of the line.
Always add your comments above (as opposed to below) the appropriate line of code.
These are displayed in "green" by default in the Code window.

' This is a comment
Dim sName As String

You can also insert a comment after an instruction on the same line.

sName = ActiveSheet.Name   ' This is also a comment  


The Comment Block and UnComment Block buttons on the Edit toolbar can be very useful for commenting out several lines of code in one step.



Comment Block -
UnComment Block -

If you are using the Edit toolbar to comment out lines of codes, you only have to place the cursor anywhere on the line for the line to be commented out.
There are no shortcut keys for these two operations although they can be added to a menu and given menu accelerator keys.


Subroutine Comment Block

Comments are commonly used to describe your subroutines.
You should always add a comment block above every procedure and function.
For more details refer to the following page: Procedures & Functions > Comment Block


Adding Comment Shortcut Key

Right click on any of the toolbars and select Customise
Select the Commands tab and select the Edit category on the left hand side.
Drag the "Comment Block" and "Uncomment Block" commands to a menu (not a toolbar).
Right click on each one and prefix it with a "&"
&Comment Block
&Uncomment Block
Check the image and text
Close - SS


Rem Keyword

The apostrophe (') is the preferred comment indicator, although you can use the "Rem" keyword if you want.
This is a rollover from the BASIC days.
Until the apostrophe the Rem keyword can only be written at the start of line and not on the same line (after an instruction).


Using Conditional Compilation Arguments

You could alternatively put your comments in between Debugging > Conditional Compilation Arguments.

#IF debug = 1 Then 
' this is my comment
#EndIf


Before a Procedure and Function

A specification should provide an indication to what the subroutine is doing.


'*********************************************************************
'Action: Reads a Text File

'Arguments: sOne
'sTwo

'Returns:

'Date, Developer, Change:

'*********************************************************************
Public Sub MySubroutine(ByVal sOne As String, _
                    ByVal sTwo As String)

End Sub


Important

Don't comment for the sake of it or bother commenting the obvious.
The compiler will ignore any text (up until the end of the line) that follows an apostrophe unless the aprostrophe is contained inside a string.
When you are testing a subroutine and you want to ignore certain lines of codes you can just temporarily change them into comments.


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

PrevNext