Professional Documents
Culture Documents
Joseph Billo
Copyright 2001 by John Wiley & Sons, Inc.
ISBNs: 0-471-39462-9 (Paperback); 0-471-22058-2 (Electronic)
15
WORKING WITH ARRAYS IN VBA
Many scientists make extensive use of arrays in their calculations. Because
arrays in VBA can be quite confusing, this chapter provides detailed coverage
of this important topic.
DIMENSIONING AN ARRAY
The Dim statement is used to declare the size of an array. Unless specified
otherwise, VBA arrays begin with an index of 0. The statement
279
280 Excel for Chemists
It’s considered good programming practice to put the Dim statements at the
beginning of the procedure.
MULTIDIMENSIONAL ARRAYS
Arrays can be multidimensional. Excel permits arrays with up to 60
dimensions; one-, two- and three-dimensional arrays are common. To create a 2-
D array with dimensions 2 x 500, use the statement
DYNAMIC ARRAYS
If you don’t know what array size you will need to handle a particular
Chapter 15 Working with Arrays in VBA 281
problem, you can create a dynamic array. This will allow you to declare a
variable as an array but set its size later. Dimension the array using the Dim
command, using empty parentheses; use the ReDim command later to specify
the array size, e.g.,
Dim BothMeans(lOO0, 2)
If you use Preserve, you can’t use the ReDim command to change the
number of dimensions of an array.
282 Excel for Chemists
Sub ArrayDemo ()
‘Reads values from column A, beginning in row 2,
‘into a VBA array.
I++++++++++++++++++++++++++++++++++++++++
Dim TestArray
LastRow = Range(“A2”).End(xlDown).Row
NCells = LastRow - 1
ReDim TestArray(NCells)
For x = 2 To LastRow
TestArray(x - 1) = Range(“A” & x)
Next x
MsgBox “First array element = ” & TestArray(1) & Chr(13) & _
“Last array element = ” & TestArray(NCells)
End Sub
Figure 15-l. Reading values into a VBA array.
Chapter 15 Working with Arrays in VBA 283
TestArray = Range(“A2:AlO”)
The worksheet array can be a range reference or a name that refers to a
reference. Thus, if the name XRange had been assigned to the range ” A2 : A 10 ‘I,
then the following expression would also create a VBA array called TestArray:
TestArray = Range(“XRange”)
A one-row or one-column reference becomes a one-dimensional array; a
rectangular range becomes a two-dimensional array of dimensions a rray(ro ws,
columns).
The lower index of these arrays is always 1. Although arrays created
within VBA have a lower array index of zero unless specified otherwise (by
means of the Option Base 1 statement, for example), when you transfer a
range of cells from a worksheet to VBA, an array is created with lower arrav J
index of 1.
Sub ArrayDemo
‘Reads values from column A, beginning in row 2,
‘into a VBA array.
I++++++++++++++++++++++++++++++++++++++++
TestArray = Range(“A2:AlO”)
MsgBox “Fifth array element = ” & TestArray(5, 1)
Msg Box “Now changing first array element to zero.”
TestArray(5, 1) = 0
Msg Box “Confirming that first array element = ” & TestArray(5, 1)
MsgBox “Please examine first array element on worksheet.”
End Sub
Figure 15-2. Another way to create a VBA array.
Note that the values in the range of cells have not been transferred to a
VBA array; the VBA variable simply “points” to the range on the worksheet.
or
ar.Columns.Count.
Sub ArrayDemo2aO
‘Reads values from column A, beginning in row 2,
‘into a VBA array of object variables.
I++++++++++++++++++++++++++++++++++++++++++
Set TestArray = Range(“A2:AlO”)
,MsgBox “Fifth array element = ” & TestArray(5)
MsgBox “Now changing fifth array element to zero.”
TestArray(5) = 0
Msg Box “Confirming that fifth array element = ” & TestArray(5)
MsgBox “Please examine fifth array element on worksheet.”
lend Sub
Figure 15-3. Creating an array of object variables.
Chapter 15 Working with Arrays in VBA 285
Sub ArrayDemo
‘Demo to illustrate writing array values to a sheet
‘by means of a loop.
I++++++++++++++++++++++++++++++++++++++++++
D i m TestArray( 10)
‘Puts the numbers 11, 12, 13... in a VBA array.
For x = 1 To 10: TestArray = 10 + x: Next
‘Then writes the array elements to cells Dl :DlO.
For x = 1 To 10
Cells(x, 4).Value = TestArray
Next x
End Sub
Figure 15-4. Passing values from a VBA array to a worksheet.
Sub ArrayDemo
‘Demo to illustrate writing array values to a sheet
‘by writing the array.
I++++++++++++++++++++++++++++++++++++++++++
TestArray = ActiveSheet.Range(“Al:B5000”)
Range(“D1 :E5000”) = TestArray
End Sub
Figure 15-5. Another way to write values from a VBA array to a worksheet.
A&DIMENSIONALARRAYASSIGNEDTOAWORKSHEETRANGE
CAN CAUSE PROBLEMS
Arrays can cause some confusion when you write the array back to a
worksheet by assigning the value of the array to a worksheet range.
VBA considers a one-dimensional array to have the elements of the array
in a row. This can cause problems when you select a range of cells in a column
and assign an array to it, as in the Sub procedure shown in Figure 15-6.
Sub ArrayDemo
‘Illustrates that 1-D VBA arrays have elements in a row,
‘not a column.
‘Was supposed to put the numbers 11, 12, 13... in El :ElO.
‘But instead writes element(l) in all cells.
I++++++++++++++++++++++++++++++++++++++++++
D i m TestArray( 10)
‘Puts the numbers 11, 12, 13... in a VBA array.
For x = 1 To 10: TestArray = 10 + x: Next
‘Then writes the array elements to cells E1:ElO.
Range(“E1 :El O”).Value = TestArray
End Sub
Figure 15-6. From a VBA array to a worksheet: the row-column problem.
If you run the preceding Sub procedure, you will find that cells El througn
El 0 will all contain 11, the first element of the array. However, if you write
the array to a row of cells instead of a column, thus:
Range(“E1 :Nl”).Value = TestArray
each cell of the range will receive the correct array value.
There are at least three ways to “work around” this problem caused by
“horizontal” and “vertical” arrays. One way is to use a loop to write the
elements of the array to individual worksheet cells in a column (Figure 15-7),
instead of using the more convenient procedure of assigning the array variable
to a range of cells in the worksheet.
Chapter 15 Working with Arrays in VBA 287
Sub ArrayDemoG()
‘One method to “work around” the row-column problem:
‘write the array elements using a loop.
I+++++++++++++++++++++++++++++++++++++++++++++++
Dim TestArray(l0)
‘Puts the numbers 11, 12, 13... in a VBA array.
For x = 1 To 10: TestArray = IO + x: Next
‘Then writes the array elements to cells El :ElO.
DestRow = 0 : DestCol = 5
For x = 1 To 10
CeIIs(DestRow + x, DestCol) = TestArray
Next
End Sub
Figure 15-7. One method to “work around” the row-column problem.
A second way is to specify both the row and the column dimensions of the
array, so as to make it an array in a column, as illustrated in the Sub procedure
shown in Fiare 15-8.
Sub ArrayDemo
‘Second method to “work around” the row-column problem:
‘specify the row and column dimensions.
I+++++++++++++++++++++++++++++++++++++++++++++++
‘Puts the numbers 11, 12, 13... in a VBA array.
Dim TestArray(l0, 1)
For x = 1 To 10: TestArray(x, 1) = 10 + x: Next
‘Then writes the array elements to cells El:ElO.
Range(“E1 :El O”).Value = TestArray
End Sub
Figure 15-8. A second way to “work around” the rowxolumn problem.
Sub ArrayDemo
Another method to “work around” the row-column problem: use
Transpose. Note that Transpose creates a l-base array.
+++++++++++++++++++++++++++++++++++++++++++++++
Dim TestArray(l0)
‘Puts the numbers 11, 12, 13... in a VBA array.
For x = 1 To 10: TestArray = IO + x: Next
NewArray = Application.Transpose(TestArray)
Range(“E1 :El O”).Value = NewArray
End Sub
Figure 15-9. A third way to “work around” the row-column problem.
288 Excel for Chemists
passes the worksheet ranges XValues and Yvalues to the VBA procedure where
they become arrays.
A one-row or one-column reference becomes a one-dimensional array; a
rectangular range becomes a two-dimensional array of dimensions array(rows,
columns).
The Function procedure in Figure 15-10 is identical to Excel’s INDEX
worksheet function: it passes a range and a number as arguments and returns the
xth element of the array.
The name of the array in the procedure must be the name of the placeholder
argument of the function.
Chapter 15 Working with Arrays in VBA 289
IFunction ArrayDemog(ArrayRef, x)
(‘Shows how to pass an array as an argument.
I+++++++++++++++++++++++++++++++++++++++++++++++
ArrayDemo = ArrayRef(x)
End Function
Figure 15-10. Passing an array as an argument in a Function procedure.
Function ArrayDemo 10
‘Shows one way to return an array of values:
‘Create an actual array by using the Dim statement.
I+++++++++++++++++++++++++++++++++++++++++++++++
Dim ReturnValu(2, 2)
ReturnValu(1, 1) = “a”
ReturnValu(1, 2) = “b”
ReturnValu(2, 1) = “c”
ReturnValu(2, 2) = “d”
ArrayDemo 1 = ReturnValu()
End Function
Figure 1541. A Function procedure that returns an array.
Function ArrayDemo 20
‘Shows another way to return an array of values: the Array keyword
‘Array returns a single variable of Variant type
‘that contains an array.
‘Only 1 -dimensional array, though.
I+++++++++++++++++++++++++++++++++++++++++++++++
MyArray = Array(“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, _
II II “Aug”, “Sep”, “Ott”, “Nov”, “Dee”)
Jul
ArrayDemo 1 = MyArray
End Function
Figure 1542. Using the Array keyword in a Function procedure.
Function ArrayDemo 30
‘Shows another way to return a 2-D array of values.
‘Use the Array function in a nested fashion.
, ArrayDemo 3 = Array(Array(“a”, “b”), Array(“c”, “d”))
End Function
Figure 1543. Using the Array keyword to return a 2-D array.