0
ActiveSheet.Evaluate("=SUMPRODUCT((""" & Source1_Val & """ = " & Target1 & ")*(""" & Source2_Val & """ = " & target2 & ")*ROW(" & Target1 & "))")

当我直接输入 Excel 时,相同的公式有效,但通过 VBA 完成时,我没有得到任何结果。

Excel公式:

=SUMPRODUCT((Source!Q2=Q1:Q33)*(Source!L2 = L1:L33)*ROW(L1:L33))
4

3 回答 3

0

您在寻找MSDN - 使用范围吗?

Range("AD:AD").Select
Range("AE:AE").Select
于 2013-06-04T23:36:59.180 回答
0

Column1 等是否都是已定义的名称?然后:

.Evaluate("=SUMPRODUCT((Column1=Source1_Val)*(Column2=Source2_Val)*ROW())")
于 2013-06-04T23:46:48.450 回答
0

答案相当愚蠢。从文本中删除=符号。

假设单元格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
于 2013-06-13T17:43:51.793 回答