VBA function w/ arrays, loops & if statements not working in Excel 2010 on Win 8

MikeK87

New Member
Joined
May 9, 2013
Messages
1
VBA function w/ arrays, loops & if statements not working in Excel2010 on Win 8<o:p></o:p>
<o:p></o:p>
OK, so I amwriting this function to place values in cells for ONLY the dates when thetransaction will be. I.E. I am making a "weekly" contributionto my savings account, so this would occur on date X, date X+7, etc up to dateX+28 (I only have 30 days of record in my "cash flow" tab. Itried this using "sumif" and "sumifs", but wasunsuccessful, as there are some "or" conditions that have to bemet. So I decided to write my own function below. I have writtensome functions before but nothing this complex. This is the data I have which will be sent to the formula:<o:p></o:p>
<o:p> </o:p>

Pay Schedule<o:p></o:p>

Payment Amnt<o:p></o:p>

Next Due Date<o:p></o:p>

Monthly<o:p></o:p>

$ 202.87 <o:p></o:p>

5/25/2013<o:p></o:p>

Quarterly<o:p></o:p>

$ - <o:p></o:p>

N/A<o:p></o:p>

Monthly<o:p></o:p>

$ 169.46 <o:p></o:p>

5/17/2013<o:p></o:p>

Monthly<o:p></o:p>

$ 25.00 <o:p></o:p>

N/A<o:p></o:p>

Weekly<o:p></o:p>

$ - <o:p></o:p>

N/A<o:p></o:p>

Weekly<o:p></o:p>

$ - <o:p></o:p>

N/A<o:p></o:p>

Weekly<o:p></o:p>

$ 100.00 <o:p></o:p>

5/10/2013<o:p></o:p>

Monthly<o:p></o:p>

$ 200.00 <o:p></o:p>

5/14/2013<o:p></o:p>

<tbody>
</tbody>
<o:p></o:p>
Option Explicit<o:p></o:p>
Option Base 1<o:p></o:p>
Function ret_value(pay_sched, trans_dates, trans_amounts, current_date)<o:p></o:p>
Dim Result() As Variant 'Resulting Array Of Values<o:p></o:p>
Dim Final As Double 'Final Value To Be Displayed In Cell<o:p></o:p>
Dim i As Integer 'Transaction Dates Array Size<o:p></o:p>
Dim j As Integer 'Transaction Amounts Array Size<o:p></o:p>
Dim count As Integer 'Counter<o:p></o:p>
i = Application.CountA(trans_dates)<o:p></o:p>
j = Application.CountA(trans_amounts)<o:p></o:p>
count = 1<o:p></o:p>
Final = 0<o:p></o:p>
Erase Result()<o:p></o:p>
If i = j Then<o:p></o:p>
For count = 1 To i<o:p></o:p>
If pay_sched(count) ="Weekly" And (current_date = trans_dates(count) _<o:p></o:p>
Or current_date =trans_dates(count) + 7 Or current_date = trans_dates(count) + 14 _<o:p></o:p>
Or current_date =trans_dates(count) + 21 Or current_date = trans_dates(count) + 28) Then<o:p></o:p>
Result(count) =trans_amounts(count)<o:p></o:p>
Else<o:p></o:p>
Result(count) = 0<o:p></o:p>
End If<o:p></o:p>
Next count<o:p></o:p>
count = 1<o:p></o:p>
For count = 1 To i<o:p></o:p>
Final = Final + Result(count)<o:p></o:p>
Next count<o:p></o:p>
ret_value = Final<o:p></o:p>
Else<o:p></o:p>
ret_value = "Error"<o:p></o:p>
End If<o:p></o:p>
End Function<o:p></o:p>
<o:p></o:p>
The “pay schedule would be entered into the “pay_sched” array, the nextdue date would be entered into the “trans_dates” array, the payment amountswould be entered into the “trans_amounts” array. The “current_date” represents the date I amadding the values together for (i.e. if 2 transactions occur on the same day, Iwant to add them, and display the total number in that 1 cell). I want the formula to output a single number(NOT AN ARRAY), so that I can use this for each day in the 30 day cash flowthat I have put together already. The formula currently only displays a #VALUE! error in the excel sheet. I tried to see where the function breaks down, but it seems to not be running through the loops properly (it will go through the loops the correct amount of times if I do NOTHING inside that section of the loop, i.e. comment out the formula).
<o:p></o:p>
Thanks in advance for any help you can provide!<o:p></o:p>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top