Microsoft Office Tutorials and References
In Depth Information
The following expression performs a logical And operation. The MsgBox statement displays
True only when Sheet1 is the active sheet and the active cell is in Row 1. If either or both of
these conditions aren’t true, the MsgBox statement displays False .
MsgBox ActiveSheet.Name = “Sheet1” And ActiveCell.Row = 1
The following expression performs a logical Or operation. The MsgBox statement displays True
when either Sheet1 or Sheet2 is the active sheet.
MsgBox ActiveSheet.Name = “Sheet1” Or ActiveSheet.Name = “Sheet2”
An array is a group of elements of the same type that have a common name. You refer to a
specific element in the array by using the array name and an index number. For example, you can
define an array of 12 string variables so that each variable corresponds to the name of a month. If
you name the array MonthNames , you can refer to the first element of the array as
MonthNames(0) , the second element as MonthNames(1) , and so on, up to
You declare an array with a Dim or Public statement, just as you declare a regular variable. You
can also specify the number of elements in the array. You do so by specifying the first index
number, the keyword To , and the last index number — all inside parentheses. For example, here’s
how to declare an array comprising exactly 100 integers:
Dim MyArray(1 To 100) As Integer
When you declare an array, you need specify only the upper index, in which case VBA
assumes that 0 is the lower index. Therefore, the two statements that follow have the
Dim MyArray(0 to 100) As Integer
Dim MyArray(100) As Integer
In both cases, the array consists of 101 elements.