Lecture Note
University:
California State University, NorthridgeCourse:
ME 309 | Numerical Analysis of Engineering SystemsAcademic year:
2014
Views:
83
Pages:
13
Author:
u1cen279b
=
End Function
Function vCyl ( R as Double, H as Double ) As _
Double
vCyl = 4 * atn(1) * R^2 * H
End Function
• Use by cell entries in worksheet
=vCyl( B1, B2)
=vCyl( 1, 20)
=vCyl( radiusName, heightName)
• Call from other VBA procedures
V = vCyl( radius, height)
cylVol = vCyl( 1, 20)
v10cyls = 10 * vCyl( rad, hgt)
55
Arrays
56
Two-dimensional Arrays
• Arrays can be visualized as data on an
experimental variable
– Could describe pressure data points
mathematically as P1, P2, etc.
– In VBA we can represent these data points
as P(1), P(2), etc.
– We call the numbers (1, 2, etc.) indices or
subscripts
• We can use constants or variables for the
subscripts: P(4), P(k), where k has a value
Consider an experiment where you vary the current
over six levels, the voltage over four levels and
measure the efficiency, e, of an electromechanical
device. The data for each combination of current
and voltage can be represented as shown below
I(1)
I(2)
I(3)
I(4)
I(5)
V(1)
e(1,1) e(1,2) e(1,3) e(1,4) e(1,5) e(1,6)
V(2)
e(2,1) e(2,2) e(2,3) e(2,4) e(2,5) e(2,6)
V(3)
e(3,1) e(3,2) e(3,3) e(3,4) e(3,5) e(3,6)
V(4)
e(4,1) e(4,2) e(4,3) e(4,4) e(4,5) e(4,6)
57
Declaring Arrays
I(6)
58
Dimensioning Arrays
• Arrays must be declared as arrays
• The maximum size of the array must be
specified in the Dim statement
• In VBA the lowest array subscript is
zero by default
– Can use Option Base 1 in declarations
section to change default lowest subscript
to one
• Can also set lowest subscript on each
individual array
59
• Can declare arrays as follows
Dim I(1 to 6) as double
Dim V(1 to 4) as double
Dim e(1 to 4, 1 to 6) as double
• Size below depends on Option Base
What is lowest subDim I(6) as double
script for these arrays?
Dim V(4) as double
Zero or one depending
Dim e(4, 6) as double on Option Base
60
10 Using Arrays
Two-Dimensional Arrays
• Arrays components are referenced by
their subscripts
• This is often done in a For loop
For k = 0 to 100
x(k) = sin(k * PI / 100) ‘PI=3.14…
Next k
• x is an array with 101 components giving
sin(x) for 0 x p, with Dx = p/100
61
Dynamic Arrays
• Use nested for loops
– Use example of current and voltages
For k = 1 to 4
For j = 1 to 6
Power(k,j) = I(j) * V(k)
Next j Recall table:
V was in rows
Next k
I was in columns
Power(k,j) is Power(row, column)
Are k and j indices correct?
62
Passing Arrays to Procedures
• What if you do not know array size until
program is actually running?
• Use Dim a() to tell compiler that a is an
array then use ReDim with actual
dimensions
Sub getArray( N as long) as Variant
Dim x() as Double : ReDim X(1 to N)
• Can go from Dim a() as Double to any
size ReDim: ReDim a(1 to 10, 6 to 12)
63
Determining Array Bounds
• Declare array in argument list with
parentheses to indicate array
Use this for any
Sub mine( A() as double)
size array.
‘No dim statement for A
Variant arrays
A(2,3) =
do not need ()
• Calling program sets actual dimensions
on array and uses only the following
Dim B(1 to 10, 1 to 6) as double
Call mine(B)
64
Worksheet Arrays to VBA
• The UBound and LBound functions
determine the upper and lower bounds
of unknown array dimensions
• For a two-dimensional array, A(m,k)
• Passed as a range of cells
• First step is to set a type variant variable equal to the input range variable
– The variant variable is now an twodimensional array
– May have single row or single column, but
is still a two-dimensional array
– Lower bound is always one
– Can use UBound to get sizes
– LBound(A,1) is the lower bound of m
– UBound(A,1) is the upper bound of m
– LBound(A,2) is the lower bound of k
– UBound(A,2) is the upper bound of k
65
66
11 Worksheet Array Example
Worksheet Array Example II
Function getMean (Ain As Range) _
As Double
Dim A as Variant, m as Long, k as Long
Dim sum as double, cells as Long
Dim nRows as Long, nCols as Long
A = Ain : nRows = UBound(A,1)
nCols = UBound(A, 2) : cells = nRows * nCols
For k = 1 to nRows
For m = 1 to nCols
Code from red line to
sum = sum + A(k,m)
end on next slide
Next m
Dim sum as double, cells as Long
Dim nRows as Long, nCols as Long
A = Ain : nRows = UBound(A,1)
nCols = UBound(A, 2) : cells = nRows * nCols
For k = 1 to nRows
For m = 1 to nCols
sum = sum + A(k,m)
Next m
Next k
getMean = sum / cells
End Function
67
68
Function array2wks() As Variant
Dim userRows As Long
Dim userColumns As Long
Dim workArray() as Double
'Statements below determine rows and columns
userRows = Application.Caller.Rows.Count
userColumns = Application.Caller.Columns.Count
ReDim workArray(1 to userRows, 1 to userColumns)
VBA Array to Worksheet
• VBA steps to return array to worksheet
– Declare the function type as Variant
– In the function or sub declare a working
array for calculations
• Use application.caller for dimensions
– Write the code for values in working array
– At end of function set =
‘Place code here to compute all
‘components of workArray
• To use the function: select cells; enter
function in formula bar; Cont+Shift+Enter
69
Passing by Reference/Value
array2wks = workArray
End Function
70
Passing by Reference/Value 2
• Consider the following function call
Call mySub( a, b)
Sub mySub( x as Long, y as Long)
x=2*x
Answer: The value of a
will have the new value
y=x/y
of x computed in myFunc
End Sub
• By default VBA passes memory
locations of variables to procedures
– This is known as pass by reference
• Alternative is pass by value
• What happens to the value of a in the
calling program because of the x = 2 * x?
71
– This simply sends the procedure the value
stored in the memory location
– To use pass by value enter the keyword
ByVal before the variable in the header
Sub mySub( Byval x as Long, y as Long)
72
12 Strings
Debugging
• Consider only variable length
• Use Dim str as String
• Use & or + as concatenation operator to
join two strings
• Len(str) gives length of string
• Left, Right, and Mid give substrings in
same manner as worksheet functions
• InStr function searches for substrings
73
• Debugger allows you to step through a
program and see intermediate values
– Useful to find location of errors
• Items to use in debugger
– Breakpoints stop execution at certain points
– Step-by-step execution
– Intermediate and Watch windows
– Hover mouse over variable to get its value
– Change statement to be executed next
74
Help
• Help systems for Excel and VBA
• Search function does not always return
what you are looking for
• If you know the keyword, type it, place
the cursor in the keyword, and press F1
• Sometimes a Google search for “VBA
” works
75
ME 309 – Numerical Analysis of Engineering Systems
13
Numerical Analysis of Engineering Systems
Get your assignment done in just 3 hours. Quick, easy, and available 24/7.
Report
Tell us what’s wrong with it:
Thanks, got it!
We will moderate it soon!
Our EduBirdie Experts Are Here for You 24/7! Just fill out a form and let us know how we can assist you.
Enter your email below and get instant access to your document