Wednesday, 18 April 2018

Using Comments in Your VBA Code

Using Comments in Your VBA Code

A comment is the simplest type of VBA statement. Because VBA ignores these statements, they can consist of anything you want. You can insert a comment to remind yourself why you did something or to clarify some particularly code you wrote. Use comments liberally and extensively to describe what the code does (which isn’t always obvious by reading the code itself). Often, code that makes perfect sense today mystifies you tomorrow.

You begin a comment with an apostrophe ('). VBA ignores any text that follows an apostrophe in a line of code. You can use a complete line for your comment or insert your comment at the end of a line of code. The following example shows a VBA procedure with three comments, although they’re not necessarily good comments:

Example

Sub CommentDemo()
'  This procedure does nothing of value
   x = 0   'x represents nothing
   'Display the result
   MsgBox x
End Sub

The “apostrophe indicates a comment” rule has one exception. VBA doesn’t interpret an apostrophe inside a set of quotation marks as a comment indicator. For example, the following statement doesn’t contain a comment, even though it has an apostrophe:

Example

  MsgBox = “Can’t continue.”

When you’re writing code, you may want to test a procedure by excluding a particular statement or group of statements. You could delete the statements and then retype them later. But that’s a waste of time. A better solution is to simply turn those statements into comments by inserting apostrophes. VBA ignores statements beginning with apostrophes when executing a routine. To reactivate those “commented” statements, just remove the apostrophes.

Here’s a quick way to convert a block of statements to comments. In the VBE, choose “View ⇨ Toolbars ⇨ Edit” to display the Edit toolbar. To convert a block of statements to comments, select the statements and click the Comment Block button. To remove the apostrophes, select the statements and click the Uncomment Block button.

Although comments can be helpful, not all comments are created equal. For example, the following procedure uses lots of comments, but they add nothing of value. In this case, the code is clear enough without the comments.

Example

Sub BadComments()
'  Declare variables
   Dim x As Integer
   Dim y As Integer
   Dim z As Integer
'  Start the routine
   x = 100    'Assign 100 to x
   y = 100    'Assign 100 to y
'  Add x and y and store in z
   z = x + y
'  Show the result
   MsgBox z
End Sub

Everyone develops his or her own style of commenting. To be useful, however, comments should convey information that’s not immediately obvious from reading the code. Otherwise, comments just chew up bytes and make files larger than necessary.

The following tips can help you make effective use of comments:

  1. Briefly describe the purpose of each Sub or Function procedure you write.
  2. Use comments to keep track of changes you make to a procedure.
  3. Use a comment to indicate that you’re using a function or a construct in an unusual or nonstandard manner.
  4. Use comments to describe the variables you use, especially if you don’t use meaningful variable names.
  5. Use a comment to describe any workarounds you develop to overcome bugs in your code.
  6. Write comments as you develop code, instead of saving the task for a final step.
  7. Depending on your work environment, consider adding a joke or two as a comment. The person who takes over your job when you get promoted might appreciate the humour.

No comments:

Post a Comment