Page 73 - Excel Progamming Weekend Crash Course
P. 73

d540629 ch04.qxd  9/2/03  9:28 AM  Page 48




                48                                                            Friday Evening

                  A VBA program is composed of statements. There is usually a single statement per line of
               code. There are two exceptions to this:
                   Multiple statements can be separated by a colon and placed on the same line.
                   A single statement can be broken over two or more lines with the line continuation
                   character (a space followed by an underscore).
                  Using the first technique to place multiple statements per line is not recommended
               because it provides no real advantage and can make the code more difficult to read and
               understand.
                  On the other hand, use of the line continuation character can be a real help. Some lines of
               VBA code are quite long and will extend past the right edge of the editing window, making
               viewing and editing the code difficult. To break a line, type a space and then an underscore,
               and press Enter. The only place you cannot use the line continuation character is within a
               quoted string. I recommend indenting each continuation line with respect to the first line.
               VBA does not care about this, but it makes it more obvious to anyone reading the code that
               these are continuation lines and not independent lines.

               Comments

               A comment is text in a VBA program that is ignored and has no effect on the program oper-
               ation. You can (and should) use comments to document how the code works, provide details
               about procedure arguments, and add reminders to yourself or another programmer.
                  One way to create a comment is with the apostrophe (single quote) character. Anything
               following an apostrophe, to the end of the line, is considered a comment. The apostrophe
               can be at the start of the line, or in the middle:
                  ‘ This is a comment.
                  ‘ Dim MyWB As Workbook ‘This is a comment too.
                  You can also use the REM keyword (standing for “remark”) to mark a comment. Note that
               REM must be at the start of a line:
                  REM This is a comment.
                  Dim MyWB As Workbook REM This is not permitted
                  You’ll see that the VBA Editor displays comments in a different color — green by default.
                          You can use the apostrophe to “comment out” code. This can be useful dur-
                          ing development when you want to see how a program runs without one or
                   Tip    more existing lines of code. Rather than deleting the lines, you can convert
                          them to comments. You can easily uncomment the code later if you decide
                          to keep it.
   68   69   70   71   72   73   74   75   76   77   78