PDA

View Full Version : Problem calling function twice



outerspace
01-02-2010, 10:22 AM
I am having a problem with the following function I have written in VBA.

Function Price(GCurve As Variant, Timeline As Variant, nsims As Integer)

Dim k As Integer
Dim nc As Integer
Dim SimRate As Double
ReDim Output(1 To 1, 1 To nsims) As Double

nc = GCurve.Rows.Columns.Count
SimRate = 0

tempCurve = GCurve

For k = 1 To 3

Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
SimRate = SimRate + Dummy(1, 11)
Output(1, k) = SimRate / k

GCurve = tempCurve

Next k

Price = Output
If I replace the loop by from k=1 to 1 (or from k=2 to 2) everything works fine. I have isolated the problem to the lines

Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
I can't see why I shouldn't be allowed to call this function twice, even within another function. SimCurve returns an array, and the structure is as follows:

Function SimCurve(FCurve As Variant, Timeline As Variant, _
tstep As Double, nsteps As Integer) As Variant
(alter FNew based on input)
FCurve = FNew
SimCurve = FCurve

End Function
Any ideas as to what could be going wrong? :)

Simon Lloyd
01-02-2010, 11:55 AM
Where does this appear?
Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
and what purpose would it serve by assigning the variable "Dummy" to the same function twice?

outerspace
01-02-2010, 12:38 PM
Good call!

As you can see from the original code, it is actually not assigning the same value twice, since SimCurve is running a simulation, and not returning the same value each time. Then I would like to aggregate the values I obtained.

I noticed that calling the function using named ranges instead works, but it is a rather clumsy solution, and I would prefer to use vectors if possible.


When running the loop, that statement will be executed thrice (although not in succession)

mikerickson
01-02-2010, 05:24 PM
Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
SimRate = SimRate + Dummy(1, 11) from this, can we conclude that Dummy is a Variant and that the function SimCurve returns a Variant array?

Paul_Hossler
01-03-2010, 07:05 AM
outerspace -- maybe an example WB with data and how you'reusing the VBA would help

Paul