Consulting

Results 1 to 6 of 6

Thread: Need help with a function called PAYBACK

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location

    Need help with a function called PAYBACK

    Chitosunday posted some code a while back called PAYBACK, which I have inserted below. It helps to determine the number of periods it takes to recoup an initial investment. I have a similar use for the function, but have a little twist: My future periods array (finflow) is in non-contiguous cells. The code as written works fine when these cells all run together, but when I break them up -- as I need to do to separate years -- the code factors in the cells between the defined ranges and returns an incorrect value. This problem only occurs when the code has to jump from one block of the array to the next. I need to know if there is a way to have the code recognize only the defined cell array for the future period data. Help please?

    Function Payback(invest, finflow)
    x = Abs(invest)
    i = 1
    c = finflow.Count
    Do
        x = x - v
        v = finflow.Cells(i).Value
        If x = v Then
            Payback = i
            Exit Function
        ElseIf x < v Then
            P = i - 1
            Z = x / v
            Payback = P + Z
            Exit Function
        End If
        i = i + 1
    Loop Until i > c
    Payback = "> " & c
    End Function

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is a bit tricky without any sample data and with no declared variables.
    Best guess (untested)
    [vba]Function Payback(invest, finflow)
    Dim Arr(), Cnt As Long, i As Long
    Cnt = WorksheetFunction.Count(finflow)
    ReDim Arr(Cnt - 1)
    For Each cel In finflow
    If cel <> "" Then
    Arr(i) = cel.Value
    i = i + 1
    End If
    Next

    x = Abs(invest)
    i = 1
    c = Cnt
    Do
    x = x - v
    v = Arr(i-1)
    If x = v Then
    Payback = i
    Exit Function
    ElseIf x < v Then
    P = i - 1
    Z = x / v
    Payback = P + Z
    Exit Function
    End If
    i = i + 1
    Loop Until i > c
    Payback = "> " & c
    End Function
    [/vba]
    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'

  3. #3
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location
    Sorry about the lack of data to work with. Yes, that would have been a good thing to include. (I've put some in below.) I tried the additional code you supplied, but got the same result. Once the code is in the Do loop, it seems to count every cell from the first one defined to the last one defined, regardless of whether or not you have the individual intervening cells defined/selected in the array. It's as though it is bypassing some standard, underlying Excel functionality.

    Let me give you some dummy values to play with.
    The 1st row is row headers.
    The 2nd row is my inventory (invest).
    The 3rd row are sales against the inventory (finflow).
    The 4th is the expected result of the function.
    The 5th row is the actual result. Note that the result is only incorrect when it has to pass over the blank row, which is omitted from the defined range of finflow. (This table is transposed from its typical layout. The asterisks represent the blank cells on the blank row.)

    J A S O N D blank row J F M A M J
    31 30 28 28 27 19 * 21 21 16 13 12 9
    3 5 5 4 6 13 * 2 2 8 6 5 9
    4.8 5.0 5.1 4.6 4.3 4.2 * 4.0 3.2 2.6 1.9 1.8 2.0
    4.8 6.0 6.1 5.6 5.3 4.2 * 4.0 3.2 2.6 1.9 1.8 2.0

    The inventory number is a month-end value, so the sales that are counted against it begin in the following month. For example, the July inventory of 31 will last 4.8 months based on sales of 5, 5, 4, 6, and 11/13. This calculation does not pass over the blank space and is correct. Looking at the October inventory of 28, we would expect a result of 4.6 based on sales of 6, 13, 2, 2, and 5/8. Instead, it is calculating 5.6 based on sales of 6, 13, 0, 2, 2, and 5/8. The 0 is the blank cell that is not in the defined range of finflow. I get the same result whether I include the blank cells in the defined range -- so that the range is one contiguous set of cells -- or whether I leave them out. The code doesn't seem to be able to tell the difference.

    As for the number of rows, I am looking forward 24 months, so there are never more than three blocks in the finflow array. If the result is greater than 24, the Payback result will simply return ">24".

    I appreciate your help with this. If there is any other information I can provide to help you resolve this, let me know and I will get you what you need. Thanks!
    Last edited by kualjo; 03-23-2007 at 11:13 AM. Reason: Adding sample worksheet attachment

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a sample workbook. UIse Manage Attachments in the Go Advanced section.
    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'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    See attached
    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'

  6. #6
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location
    I think I have solved the problem by simply adding a new variable that counts the number of blank cells encountered in the finflow array. When i is calculated just prior to returning Payback, the new variable is subtracted from it, thus providing the correct value.
    Thank you for your time and effort. You led me in the right direction.

Posting Permissions

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