PDA

View Full Version : Passing Values::Arrays



Saladsamurai
12-05-2009, 12:33 PM
Alrighty then :) I am trying to get all of this 'passing values' stuff sorted out and I feel like I am doing well. But I have hit a few snags that I need help with:


I have a module I am writing. One of the Subs called Sub GetAugMatrix(mSize as Integer) picks up values for an Array called 'AugMatrix' from the the Worksheet based on the size of the Worksheet Matrix..

So now I have this Array 'AugMatrix.'

The problem is that now I want to write another Sub that does some stuff to AugMatrix. This Sub will be called RREF() and it should take in an Array as its argument and return an array as well.

So I need a way of getting the Array 'AugMatrix' from Sub GetAugMatrix() into RREF().

I have a feeling that this will involve turning my 'Subs' into 'Functions' instead.

How can I change the Sub GetAugMatrix(mSize as Double) into a function so that it actually returns an Array instead of just making one?

I want the general flow of the program to be something to the effect of:

After the Command Button is clicked-->AugMatrix= GetAugMatrix(mSize)--->FinalArray = RREF(AugMatrix)

SO I guess my main question is what is the syntax to tell excel that this function should take in an array whose elements are of type double and should return an array of type double.

Here is the WrBk. Any ideas are welcome!

mikerickson
12-05-2009, 01:49 PM
These show how a function can return an array, the first takes a number as its argument. The second takes an array as its argument
Function OneTo(N As Long) As Variant
Rem returns the array {1, 2, 3,...,N}
Dim returnArray() As Long, i As Long
ReDim returnArray(1 To N)

For i = 1 To N
returnArray(i) = i
Next i

OneTo = returnArray
End Function

Function AddTenToArray(inputArray As Variant) As Variant
Rem adds 10 to each element of the input array
Dim returnArray As Variant, i As Long
ReDim returnArray(LBound(inputArray) To UBound(inputArray))

For i = LBound(inputArray) To UBound(inputArray)
returnArray(i) = inputArray(i) + 10
Next i

AddTenToArray = returnArray
End Function

Sub test()
Dim myRRay As Variant
myRRay = OneTo(8)
MsgBox Join(myRRay)
MsgBox Join(AddTenToArray(myRRay))
End Sub

Bob Phillips
12-05-2009, 01:55 PM
Subs is okay



Call RREF(AugMatrix)

'...

Public Sub RREF(ByRef Mtx As Variant)

'any changes to Mtx get returned to caller
End Sub