[英]Excel VBA - Calculate in VBA
簡單的問題。 我有一個表示公式的字符串。
喜歡
Dim formula As String
formula = Replace(processFormula(2016, 9000, constants, formulaRow), "##", 9000)
'formula has the value "(1007,27*((9000 - 8820)/10000)+1400)*((9000 - 8820)/10000)"
MsgBox (formula)
'Evaluate(formula) not working sadly
現在,我想在VBA中對其進行評估並返回結果。 我沒有進行評估工作。
processFormula是一個生成公式的函數,它返回一個String。 因此,將String放入替換函數中,該替換函數用數字替換“ ##”。 這里沒什么神奇的。 關於調試器公式,是一個具有上述值的字符串(直接從調試器復制)。
有沒有簡單易行的方法?
謝謝,我希望這不是重復的。
您應將小數點逗號替換為小數點
Dim formula As String
formula = replace(Replace(processFormula(2016, 9000, constants, formulaRow), "##", 9000),",",".")
'formula has the value "(1007,27*((9000 - 8820)/10000)+1400)*((9000 - 8820)/10000)"
MsgBox (formula)
Evaluate(formula) 'should work now ;o)
使用以下子。
Sub Calculate()
Dim x As Double
x = ((100 * 20) / 30 * 500)
Range("A1") = x
MsgBox x
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.