Consulting

Results 1 to 5 of 5

Thread: Problem calling function twice

  1. #1

    Problem calling function twice

    I am having a problem with the following function I have written in VBA.

    [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[/vba]
    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

    [vba]Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
    Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)[/vba]
    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:

    [vba]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[/vba]
    Any ideas as to what could be going wrong?

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Where does this appear?[VBA]
    Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
    Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
    [/VBA]and what purpose would it serve by assigning the variable "Dummy" to the same function twice?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    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)

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]Dummy = SimCurve(GCurve, Timeline, 1 / 20, 100)
    SimRate = SimRate + Dummy(1, 11) [/VBA]from this, can we conclude that Dummy is a Variant and that the function SimCurve returns a Variant array?

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    outerspace -- maybe an example WB with data and how you'reusing the VBA would help

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •