答案相当愚蠢。从文本中删除=
符号。
假设单元格D7:D9
以及E7:E9
包含数值,以下函数在即时窗口中运行良好
?Evaluate("SUMPRODUCT(D7:D9, E7:E9)")
编辑:假设下面的所有变量名称都是字符串。
dim myFormula as string
myFormula = "SUMPRODUCT((" & Source1_Val & " = " & Target1 & ")" _
& " * (" & Source2_Val _
& " = " & target2 & ")*ROW(" & Target1 & "))"
ActiveSheet.Evaluate(myFormula)
无需在公式中包含额外的双引号。
EDIT2:根据附件,这里是显示它有效的代码
option explicit
Sub TestFormulaStringUsingEvaluate()
Dim formula As String
Dim source1_Val As String
Dim source2_Val As String
Dim target1 As String
Dim target2 As String
'** Fill in the values for the following 4 variables
source1_Val = "Source!Q2"
target1 = "Target!Q$1:Q$10"
source2_Val = "Source!L2"
target2 = "Target!L$1:L$10"
formula = "SUMPRODUCT((" & source1_Val & "= " & target1 & ") " _
& "*(" & source2_Val & "=" & target2 & ") " _
& "* ROW(" & target2 & "))"
MsgBox Application.Evaluate(formula)
End Sub