Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.

Similar presentations


Presentation on theme: "CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and."— Presentation transcript:

1 CHAPTER 9 PART II

2 MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and column by convention. Two-dimensional: arrays that have two indexes to identify element. VBA supports at least 60 array dimensions generally use no more than 2 or 3.

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-dimensional array incorrectly.

4 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 The Column_Array will contain column totals, while Row_Array will contain 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

5 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

6 DYNAMIC ARRAYS Example: cannot change 2-dimensional to 3- dimensional First ReDim sets number of dimensions Use sparingly since it will consume processor time ReDim executed: (1) sets all numeric values to zero (2) sets string to zero-length Preserve- saves values when ReDim is used

7 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 3 rd dimension from 1 to 2) Logic error: using ReDim without Preserve and assuming array still contains previous value Run-time errors: (1) attempting to change bounds for any dimension except last in a multidimensional array (using ReDim Preserve) (2) failure to Preserve array data can result in unexpected loss of data

8 DYNAMIC ARRAYS Deallocated (released) memory: Erase will release memory at run-time (1) array must be redimensioned with ReDim before use (2) Erase used with fixed sized arrays initialize elements to zero (3) run-time error: accessing a dynamic array that has been deallocated

9 VARIABLE-LENGTH ARGUMENTS: PARAMARRAY ParamArray- indicates a variable number of arguments are received Precedes declaration of Variant array Syntax error- declare variable in procedure header to right of ParmArray Syntax error: (2) declare a non-array variable with ParamArray (3) use ParamArray with an array type other than variant

10 FUNCTION ARRAY Creates and returns Variant array at execution time Values passed to array specify element values in returned array Returned array’s lower bound either 0 or 1 depending on Option Base ParamArray used to create Variant array


Download ppt "CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and."

Similar presentations


Ads by Google