You are on page 1of 11

# 1

1
Excel & Visual Basic for Applications (VBA)
array data structures
using For...Next loops with arrays
defining chart templates
2
Array Data Structures
Example: a set of pH measurements on treated wastewater
6.2 7.1 7.2 7.6 7.1 7.5 7.0 8.5
7.0 6.5 7.0 6.8 7.7 7.8 8.1
These measurements can be stored on the spreadsheet
and they can be stored in an array variable in VBA.
In either case, it is convenient to refer to the set of
measurements by a single identifier and then refer to
individual measurements using a subscript.
pH
4
the 4
th
item in the pH array
This type of referencing is handled differently in Excel and
VBA and cross-referencing from Excel to VBA is distinct
also.
2
3
Referencing array elements on the spreadsheet
array of cells named “pH”
Use of INDEX function to
select one element of
pH array with row and
column indices
4
Referencing array elements on the spreadsheet from VBA
“Borrow” the INDEX function from Excel
Alternative:
Use the Offset property
3
5
Transferring the array from the spreadsheet to a VBA
array variable
Setting the array values within VBA
6
Building an array in VBA from user input
This is called a “sentinel” value
This extends the size of the array for each data value
entered, without wiping out the previous values entered
4
7
Once you have an array built inside a VBA program,
what do you do with it?
You name it!
Compute properties, statistics
Search for a specific value
Sort in a particular order
Make array-based or vector/matrix calculations
8
Accumulate the sum of the pH values and compute the average
5
9
Search for a specific value in the pH array
10
Sort array in descending order
6
11
Two-dimensional arrays
Examples:
12
Convention for Matrix Notation
“row-column” subscript notation
Note: Excel cell
“column-row” F4
A
23
2
nd
row, 3
rd
column
A
ij
i
th
row, j
th
column
VBA notation: A(2,3) A(i,j)
Declaring two-dimensional arrays
Dim A(5,5) As Single
7
13
Example program with a 2-dimensional array:
computing row totals and displaying on spreadsheet
14
Results from program:
Accumulating the sum of all the elements of the 2-dimensional array
8
15
Matrix math
16
Defining chart templates
[ alternative to chart macros ]
create a generic
X-Y line graph
from an example
data set
Y vs X Line Graph
-10.000
0.000
10.000
20.000
30.000
40.000
50.000
60.000
-200 0 200 400 600 800 1000 1200 1400 1600
X
Y
9
17
Select
Custom Types
tab and
User-defined
then click on the
button
18
Give the custom chart
a name and, perhaps,
a description
then click OK
It is then added to the list of custom chart types:
click OK to close the
Chart Type dialog
box
10
19
Now, create the graph again, but quickly, accepting
the defaults along the way.
-10.000
0.000
10.000
20.000
30.000
40.000
50.000
60.000
-200 0 200 400 600 800 1000 1200 1400 1600
Series1
It doesn’t look very good!
Go back to the Chart Type
dialog box and select the
XYLineGraph type, then
click OK
20
Y vs X Line Graph
-10.000
0.000
10.000
20.000
30.000
40.000
50.000
60.000
-200 0 200 400 600 800 1000 1200 1400 1600
X
Y