Consulting

Results 1 to 2 of 2

Thread: Loop inside a function to retrieve data

  1. #1
    VBAX Newbie
    Joined
    Oct 2017
    Posts
    1
    Location

    Loop inside a function to retrieve data

    I am trying to have a function that calculates de Variance of returns (ln(return2/return1)^2) of the S&P in order to have it cumulatively added and subtract it from a "budget" number.
    Once this substraction becomes equal or larger than "budget" I want it to provide me the Date in which day that happens ( on column A)
    So it looks like this
    Column A Column B Column
    01/30/2015. 2550.......................
    01/29/2015. 2600 ................Date where cumulative variance is larger or equal than Budget......
    ............
    04/25/2014. ....................................

    I have written the following code but I fail to obtain the desired result:

    Public Function strange_sum(inrange As range, budget As Double) As Long
    Dim total As Long
    Dim i As Double
    Dim w As Double
    
    i = 8
    total = 0
    
    While total < budget
    i = i + 1
    var = Log(Cells(i, 8).Value / Cells(i + 1, 8).Value)
    total = total + var
    
    If i = 2 And total < 0 Then total = 0
    
    
    
    strange_sum = Cells(i, 9).Offset(0, -1)
    
    Wend
    End Function

    I need this for work :/ id appreciate all your help.
    Thanks! ( its my first post)


    Last edited by mdmackillop; 10-10-2017 at 04:35 AM. Reason: Code tags added

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you provide a workbook sample? Go Advanced / Manage Attachments
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Tags for this Thread

Posting Permissions

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