MS-Word / General Formatting

Understanding the Basic Elements of VBA

The following sections present a whirlwind tour of some basic elements of writing VBA macros.

Rules for writing VBA statements

The normal way to write VBA statements is one per line. For example, here are two VBA statements, each on its own line:

Dim i As Integer
i = 100

If you want, you can "gang up" several statements on a single line by separating the statements with colons, as in this example:

Dim i As Integer : i = 100

However, your macros are easier to read if you stick to one statement per line.

You can indent lines to show the relationships among statements by using tabs or spaces. For example:

If Weekday(Now()) = 1 Then
    MsgBox "Time for tennis!"
End If

However, be aware that extra spaces aren't required. The following lines have exactly the same meaning as the previous example:

If Weekday(Now()) = 1 Then
MsgBox "Time for tennis!"
End If

In addition, spacing within a VBA statement generally isn't important. You can omit spaces when different elements of a statement are separated by a comma or colon or another punctuation symbol. However, the VBA editor often inserts spaces around those elements as you type them.

If you need to continue a statement onto two or more lines, end every line except the last one with an underscore character, like this:

MsgBox ("This is a really " _
    & "long message.")

The underscore character indicates to Word that the statement continues on the next line, so the successive lines are treated as a single statement.

Comments

As mentioned earlier in this tutorial, comments are marked with apostrophes. When you use an apostrophe anywhere on a line, VBA completely ignores everything on the line after the apostrophe. This treatment allows you to place comments directly on the lines where they relate:

Sub BoldItalic() ' This macro applies both bold and italic.
    Selection.Font.Bold = wdToggle     'Toggles Bold
    Selection.Font.Italic = wdToggle   'Toggles Italic
End Sub

Projects, modules, procedures, and macros

At its simplest level, a macro is a single VBA procedure that you can run from an application such as Word. However, VBA lets you gather procedures to form modules and projects to help you organize your macros. The following list describes each of these organizational units:

  • Procedure: A named collection of VBA statements that's contained between Sub and End Sub statements.
  • Macro: A specific type of procedure that you can invoke directly from a Word document. You can run a macro from the Macros dialog box or by associating it with a customized Quick Access Toolbar button or a keyboard shortcut.
  • Module: A named collection of procedures. All Word documents include a module named New Macros. When you create a macro from the Macros dialog box, the new macro is created in this module. If you're working with a lot of macros, you can create additional modules to help keep them organized.
  • Project: A collection of modules and other Word objects. Every document and template is a project.
[Previous] [Contents] [Next]