CHAPTER 9
PART II
MULTIDIMENSIONAL ARRAYS
Used to represent tables of values arranged inrows and columns.
Table element requires two indexes: row andcolumn by convention.
Two-dimensional:
arrays that have two indexes to identifyelement.
VBA supports at least 60 array dimensions
generally use no more than 2 or 3.
MULTIDIMENSIONAL ARRAYS
u(i,j)  u = array name, i and j are indexes
Example: Dim Sales(2,2) As Integer
Option Base determines lower bound of each dimension
Example: Dim tripleArray(50 to 100,8, 7 to 15)
Ubound or Lbound could also be used
The dimension is passed as second argument.
If the dimension not provided, default dimension=1.
Syntax error will occur if you reference a 2-dimensionalarray incorrectly.
MULTIDIMENSIONAL ARRAYS
Example:
For i = 1 To UBound(Array1)
For j =  To UBound(Array1,2)
Column_Array( j ) = Column_Array( j ) + Array1( i, j )
Row_Array( i ) = Row_Array( i ) + Array1( i , j )
Next
Next
The Column_Array will contain column totals, while Row_Array willcontain row totals.
Common place to use For structure in array manipulations
Access:
1-dimensional array use 1 For loop
2-dimensional array use 2 For loops
DYNAMIC ARRAYS
Redimmable-grow and shrink at run-time
Flexible and can be resized anytime
Scope: Public (code modules), module, local
Local dynamic arrays declared with Dim or Static
allows efficient management of memory
do not give size when declared
Example: Dim dynamicArray() As Double
Size is declared at run-time using ReDim
Example: ReDim dynamicArray(9)
(1) ten elements if Option Base = 0
(2) nine elements if Option Base = 1
Syntax errors:
(1) attempting to use ReDim outside a procedure
(2) attempting to use ReDim on fixed-size array
ReDim can change index bounds
Example: ReDim dynamicArray(50 to 100)
changes lower bound from 0 to 50
total number of dimensions cannot be changed
DYNAMIC ARRAYS
Example: cannot change 2-dimensional to 3-dimensional
First ReDim sets number of dimensions
Use sparingly since it will consume processortime
ReDim executed:
(1) sets all numeric values to zero
(2) sets string to zero-length
Preserve- saves values when ReDim is used
DYNAMIC ARRAYS
Example: (retains original values)
ReDim Preserve dynamicArray(50 To 150)
Example: ReDim threeD(11,8,1)
ReDim Preserve threeD(11,8,2)
(change upperbound of 3rd dimension from 1 to 2)
Logic error: using ReDim without Preserve andassuming array still contains previous value
Run-time errors: (1) attempting to change bounds forany dimension except last in a multidimensional array(using ReDim Preserve)
(2) failure to Preserve array data can result inunexpected loss of data
DYNAMIC ARRAYS
Deallocated (released) memory: Erase willrelease memory at run-time
(1) array must be redimensioned with ReDimbefore use
(2) Erase used with fixed sized arrays initializeelements to zero
(3) run-time error: accessing a dynamic arraythat has been deallocated
VARIABLE-LENGTHARGUMENTS: PARAMARRAY
ParamArray- indicates a variable number ofarguments are received
Precedes declaration of Variant array
Syntax error-  declare variable in procedureheader to right of ParmArray
Syntax error:
(2) declare a non-array variable withParamArray
(3) use ParamArray with an array type otherthan variant
FUNCTION ARRAY
Creates and returns Variant array at executiontime
Values passed to array specify element valuesin returned array
Returned array’s lower bound either 0 or 1depending on Option Base
ParamArray used to create Variant array