me 142 engineering computation i using excel arrays in functions
TRANSCRIPT
ME 142Engineering Computation I
Using Excel Arrays in Functions
Key Concepts
Using Excel Arrays in Functions
Using Excel Arrays in Functions
Using Excel Arrays in Functions Techniques used earlier in the class for
matrices may also be used to pass array information to and from a function.
This effectively allows a function to return information to more than a single cell.
Using Excel Arrays in FunctionsFunction AddVect(V1, V2)'Adds two 2-dimensional vectors
Dim AV(1 To 2)
AV(1) = V1(1) + V2(1) AV(2) = V1(2) + V2(2)
AddVect = AV End Function
Example Problem Develop a function to subtract two 3-
dimensional vectors
Example ProblemFunction VDiff(v1, v2) Dim Results(1 To 3) If v1.Count <> 3 Or v2.Count <> 3 Then MsgBox (“Error-each vector must contain 3 cells") Else For i = 1 To 3 Results(i) = v1(i) - v2(i) Next i VDiff = Results End IfEnd Function
Tips for Using Excel Arrays in Functions
Use one array variable for each vector Vector arrays are passed to function via argument list Do NOT dimension argument list variables May use .Count method to determine vector size Dim output array to store results DO dimension output array variable Set output array equal to function name to return results Select entire output area in spreadsheet before launching Use Ctrl+Shift+Enter
Homework Help ‘n Hints