Lecture 9
Lecture 9
1. Data Structuring
2. Arrays
4. Multi-Dimensional Arrays
5. Dynamic Arrays
6. Records
7. Thank You!
Data Structuring
Data Structuring
Data structuring refers to organizing data in a way that enhances its efficiency and ease of use,
similar to how structured programming (decisions and loop structures) constructs make
computer code easier to comprehend
The most basic approach to data structuring is through variable type declarations using Option
Explicit and Dim, Const and Public
statements
In addition, two additional features facilitate more efficient and concise data organization,
Arrays and Records.
2
Data Structuring
An Array is a collection of values that are accessed using a single variable name and
subscripts, with all values being of the same variable type
Records, on the other hand, allow for the storage of different variable types using a
single variable name, which is useful for
managing databases with varying types of information.
Arrays
Arrays
An Array is a set of sequentially indexed elements having the same data type.
Each element of an array has a unique identifying index number, starting at zero.
Changes made to one element of an array don’t affect the other elements.
Arrays function similarly as variables. As with variables, they are required to be declared.
The general syntax is:
Dim array_name ( array_index ) As data_type
where array name is the array’s name, array index indicates the size of the array, and data type
is the data type
4
Arrays
Declaring the data type is optional, although in many cases this will slow down the execution of
your procedure
5
Arrays
Sometimes, an array is desired to start at an index number other than zero. This can be
achieved by using the To keyword and stating the index number of the first and final
elements:
Dim array_name ( first_index To final_index ) As data_type
In cases where index numbering should start at one, the following line is added at the very start
of the module, before any other code:
Option Base 1
6
Arrays
Optionally, an array declaration may omit an argument from its parentheses to create a
“dynamic” array. The size of that array can be established later using the ReDim keyword before
its elements are assigned values. The array can be resized repeatedly using the ReDim
keyword, but its element values will be lost unless the statement includes the Preserve
keyword:
Dim array_name () As data_type
ReDim array_name ( first_index To final_index )
ReDim Preserve first_index To final_index
Begin a VBA macro module with a subroutine that declares a string array of three elements
Sub FruitArray ()
8
Storing Data in an Array
Next, insert statements to assign values for each array element Sub FruitArray ()
Dim Fruit (2) as String
Fruit (0) = " Apple "
Fruit (1) = " Banana "
Fruit (2) = " Cherry "
End Sub
9
Storing Data in an Array
Display the value of the element at any index. For example, for the first fruit (index 0):
Sub FruitArray ()
10
Storing Data in an Array
Declare another array assigning 1 as the first index and 3 as the final index:
Sub VegArray ()
11
Storing Data in an Array
Next, declare a Dynamic array, then ReDim by specifying the first and final index numbers as
1 and 3 respectively:
Sub CountryArray ()
’insert elements
Country (1) = " China "
Country (2) = " Philippines "
Country (3) = "USA "
’display values
MsgBox Country (1) & " Number 1"
End Sub
12
Using For Loop with Arrays
A For-Next loop can be used to enter values for each element as shown below:
Sub CountryArray ()
’insert elements
For i = 1 To 3
Country (i) = InputBox (" Enter a Country ")
Next
’display values
MsgBox Country (1) & " Number 1"
End Sub
13
Multi-Dimensional Arrays
Multi-Dimensional Arrays
An array created with a single index is a one-dimensional array, in which elements appear in
a single row.
Multi-dimensional arrays contain two or more indices, in which elements appear in
multiple rows.
Two-dimensional arrays are useful in Excel VBA to store row and column information, in which
the first index represents rows and the second index represents columns.
Multi-dimensional arrays are created in the same way as
one-dimensional arrays, except they must specify the size of each index as comma-
separated arguments in the declaration, like this:
Dim array_name ( final_index1 , final_index2 , final_index3 ) As data_type
14
Multi-Dimensional Arrays
Create a VBA macro module by adding an initial directive to start index numbering at one:
Option Base 1
End Sub
15
Multi-Dimensional Arrays
Next, insert statements to initialize each array element in the first index Option Base 1
Sub Array2D ()
End Sub
16
Multi-Dimensional Arrays
Next, insert statements to initialize each array element in the first index Option Base 1
Sub Array2D ()
End Sub
17
Multi-Dimensional Arrays
Next, insert statements to initialize each array element in the second index
Option Base 1
’second index
Basket (2 ,1) = " Ampalaya "
Basket (2 ,2) = " Broccoli "
Basket (2 ,3) = " Cabbage "
End Sub
18
Multi-Dimensional Arrays
’second index
Basket (2 ,1) = " Ampalaya "
Basket (2 ,2) = " Broccoli "
Basket (2 ,3) = " Cabbage "
End Sub
19
Dynamic Arrays
Dynamic Arrays
Dynamic arrays can be created by omitting the argument between the parentheses in an
array declaration.
Useful when a an array involves a set of data without a fix number of values
For this type of array, we can determine its indeces using the LBound and UBound methods,
with syntax:
LBound ( array_name , dimension )
UBound ( array_name , dimension )
where array name is the name of the array and dimension is a whole number indicating which
dimension’s lower or upper bound is returned.
20
Dynamic Arrays
Option Explicit
End Sub
21
Records
Records
Records store any type of information in a single variable. To create a Record, a special
variable type must first be established using the Type statement:
Type usertype
itemname As type
itemname As type
itemname As type
End Type
22
Records
Example:
Option Explicit
23
Records
Example:
Sub Periodic ()
24
Thank You!