This is the formula in Excel that I'm trying to convert to VBA code:
=IFERROR(IF(effDate>curDate,0,IF((curDate-effDate+1)>nDays,nDays*(nSpend/365),((nSpend/365)*(curDate-effDate+1))-IF((curDate-effDate+1)-nDays>0,nSpend/365,0))),0)
For checking working purposes here are the values for each variable:
effDate: 1/1/17 (dimmed as value)
curDate: 3/31/17 (dimmed as value)
nDays: 60
nSpend: 1600
Correct answer: 263
I tried to make it work for two days now and I retyped in about a hundred different way and none of them work. The code may work for one cell but it doesn't calculate correctly for the others. Or it doesn't work at all. Here is one of the examples of what I made earlier (name of the function is APFcst):
If effDate > curDate then
APFcst = 0
Exit Function
End if
If (curDate - effDate + 1) > nDays then
APFcst = nDays * (nSpend / 365)
Else
val1 = (nSpend / 365) * (curDate - effDatea + 1)
end if
if (curDate - effDate + 1) - nDays > 0 then
val2 = nSpend / 365
else
val2 = 0
end if
APFcst = val1 - val2
Exit Function
End if
NOTE: everything is dimmed properly, the issue that I'm having is just translating the actual formula so it'll give me the correct answer. Please help! I've been staring at this for so long that I might not see an obvious solution.
If()(the worksheet function) corresponds to the VBA functionIIF()andIFERRORcan be prefixed byApplication.WorksheetFunction.and directly used in Excel VBA.