=IF((effDate)-curDate>0,0,IF(curDate-(effDate)+1>nDays,0,nSpend/4))+IF((effDate+365/4*1)-curDate>0,0,IF(curDate-(effDate+365/$4*1)+1>nDays,0,nSpend/4))+IF((effDate+365/4*2)-curDate>0,0,IF(curDate-(effDate+365/4*2)+1>nDays,0,nSpend/4))+IF((effDate+365/4*3)-curDate>0,0,IF(curDate-(effDate+365/4*3)+1>nDays,0,nSpend/4))
effDate: 1/1/2017 (as value)
curDate: 1/31/2017 (as value)
nSpend: 1600
nDays: 60
Correct answer: 400
上面是一個很長的公式,我試圖轉換爲VBA代碼。我一直試圖做的方式很長,我試圖把它分解成更小的函數,但它並沒有給我正確的答案。我的VBA技能非常初學,所以我不確定還有什麼可以嘗試的。我一直得到錯誤的答案或根本沒有答案。將此Excel公式轉換爲VBA代碼
這是我一直想:
If effdate - curdate > 0 Then
val1 = 0
Exit Function
End If
If curdate - effDate + 1 > nDays Then
val1 = 0
Else
val1 = nSpend/4
End If
If (effDate + (365/4)) - curdate > 0 Then
val2 = 0
Exit Function
End If
If curdate - (effDate + (365/4)) + 1 > nDays Then
val2 = 0
Else
val2 = nSpend/4
End If
If effDate + (365/(4 * 2)) - curdate > 0 Then
val3 = 0
Exit Function
End If
If curdate - (effDate + (365/(4 * 2))) + 1 > nDays Then
val3 = 0
Else
val3 = nSpend/4
End If
If effDate + (365/(4 * 3)) - curdate > 0 Then
val4 = 0
Exit Function
End If
If curdate - (effDate + (365/(4 * 3))) + 1 > nDays Then
val4 = 0
Else
val4 = nSpend/4
End If
End If
APFcst = val1 + val2 + val3 + val4
End Function
我正確地變灰的一切,這是實際的轉換,我有問題。 我將不勝感激幫助!自從我開始學習VBA編碼以來,這對我來說也是一個巨大的學習。 謝謝!
感謝這幫助!我試圖應用它,但它給了我不一致/不正確的答案,因爲我試圖將函數拖到其他單元格出於某種原因 – KateLO