A VBA Language Primer

In this section, we’ll take a look at the syntax and structure of the VBA language. Entire books have been devoted to VBA, but we’ll just hit the high points here. When you’re through with this section, you’ll understand the basic building blocks of VBA and how to use them to create a program.

Statements

A statement in VBA code is like an imperative sentence in a spoken language. Each statement in a program is a command for your machine to perform. The following statement displays a message box with the text “Hello, world!”:

MsgBox "Hello, world!"

Very long statements may be broken into several lines by using the line continuation character "_". You can also use indentation to make your code more readable:

nTotalPerimeter = CalculateRectanglePerimeter(4,6) + _
                  CalculateTrianglePerimeter(3,4,5) + _
                  CalculateCirclePerimeter(8)

Comments

Comments are documentation in your code. The more you comment, the better you—and anybody else—will understand your code later. You add a comment by prefacing a line of text with a single quote, or by typing a single quote and additional text after a statement. Anything following the quote is considered a comment and will not be executed. For example:

' This is a comment that takes up an entire line.
i = 3 ' This is a comment after a statement

Variables

A variable is a name that you define to hold a value. Variables can hold basic types of data like numbers, text values, or dates, as well as more complex, structured data. In order to ...

Get Outlook 2000 in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.