Variable declarations

Variables are declared using the following syntax:

Dim NameOfVariable As DataType

The following examples illustrate the way that variables are declared in VBA programs. Declarations are usually written in a VBA program, before program execution statements begin. Notice in the following examples how comments have been attached to the right of each declaration line to improve

readability.

Dim

theTotal As Long

'declared as a long integer variable

Dim

aName As String

'variable length string declaration

Dim

fName As String * 20

'fixed length size 20 characters

Dim

lastDayOfTerm As Date

'Date variable declaration

Dim

dataSheet As Worksheet

'Worksheet object declaration

Dim

studentPresent As Boolean

'Boolean variable, (TRUE or FALSE)

Dim

TopCell As Range

'Range object declaration

Dim

theCell As Object

'generic Object declaration

Dim

salesChart As Chart

'specific object Chart declaration

0 0

Post a comment