PDA

View Full Version : Need help with a function called PAYBACK



kualjo
03-22-2007, 11:40 AM
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

mdmackillop
03-22-2007, 02:53 PM
This is a bit tricky without any sample data and with no declared variables.
Best guess (untested)
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

kualjo
03-23-2007, 06:53 AM
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!

mdmackillop
03-23-2007, 07:42 AM
Can you post a sample workbook. UIse Manage Attachments in the Go Advanced section.

mdmackillop
03-23-2007, 01:41 PM
See attached

kualjo
03-26-2007, 11:33 AM
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.