2017-07-04 99 views
1
=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編碼以來,這對我來說也是一個巨大的學習。 謝謝!

回答

0

我被這個很感興趣,看看它爲什麼不工作,我已經改寫了if > else > else邏輯到if or > else,但它實現了相同的目的。就像Doug Coats的回答所說的那樣,你可以使用DateAdd來一起處理減法/添加日期。我剛剛轉換的日期字符串翻番:

Private Sub CommandButton1_Click() 
Dim x As Date: Dim y As Date 
Dim effDate As Double 
Dim curDate As Double 
Dim nSpend As Double 
Dim nDays As Double 

x = "1/1/17" 
y = "31/1/17" 
effDate = CDbl(x) 
curDate = CDbl(y) 
nSpend = 1600 
nDays = 60 

If (effDate - curDate > 0) Or (curDate - effDate + 1 > nDays) Then 
    val1 = 0 
Else 
    val1 = nSpend/4 
End If 

If ((effDate + (365/4)) - curDate > 0) Or (curDate - (effDate + (365/4)) + 1 > nDays) Then 
    val2 = 0 
Else 
    val2 = nSpend/4 
End If 

If (effDate + (365/(4 * 2)) - curDate > 0) Or (curDate - (effDate + (365/(4 * 2))) + 1 > nDays) Then 
    val3 = 0 
Else 
    val3 = nSpend/4 
End If 

If (effDate + (365/(4 * 3)) - curDate > 0) Or (curDate - (effDate + (365/(4 * 3))) + 1 > nDays) Then 
    val4 = 0 
Else 
    val4 = nSpend/4 
End If 

MsgBox (val1 + val2 + val3 + val4) 

End Sub 
+0

感謝這幫助!我試圖應用它,但它給了我不一致/不正確的答案,因爲我試圖將函數拖到其他單元格出於某種原因 – KateLO

2

如果你知道公式,那麼你可以使用Evaluate()達到你想要的東西。

Sub Sample() 
    Dim f1, f2, f3, f4 

    f1 = "=IF((effDate)-curDate>0,0,IF(curDate-(effDate)+1>nDays,0,nSpend/4))" 
    f2 = "=IF((effDate+365/4*1)-curDate>0,0,IF(curDate-(effDate+365/4*1)+1>nDays,0,nSpend/4))" 
    f3 = "=IF((effDate+365/4*2)-curDate>0,0,IF(curDate-(effDate+365/4*2)+1>nDays,0,nSpend/4))" 
    f4 = "=IF((effDate+365/4*3)-curDate>0,0,IF(curDate-(effDate+365/4*3)+1>nDays,0,nSpend/4))" 

    '~~> Change Sheet1 to the relevant sheet code name 
    Debug.Print Sheet1.Evaluate(f1) + Sheet1.Evaluate(f2) + Sheet1.Evaluate(f3) + Sheet1.Evaluate(f4) 
End Sub 

enter image description here

+0

這很有趣。我不知道這可以做到。但是,我認爲OP根本不需要這個公式。只是想要純VBA產生相同的結果。 –