簡體   English   中英

Excel VBA-在VBA中計算

[英]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放入替換函數中,該替換函數用數字替換“ ##”。 這里沒什么神奇的。 關於調試器公式,是一個具有上述值的字符串(直接從調試器復制)。

有沒有簡單易行的方法?

謝謝,我希望這不是重復的。

應該沒有問題:

Sub eval()
    MsgBox Evaluate((100 * 20) / 30 * 500)
End Sub

在此處輸入圖片說明

匹配等效單元格公式。 請注意,這是一樣的:

   (100*20)/(30*500)

編輯#1:

這也適用:

Sub eval2()
    Dim s As String
    s = "((100 * 20) / 30 * 500)"
    MsgBox Evaluate(s)
End Sub

您應將小數點逗號替換為小數點

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM