sumproduct VBA中的公式数组

时间:2013-06-04 23:33:22

标签: excel excel-vba excel-formula vba

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))

3 个答案:

答案 0 :(得分:0)

您在寻找MSDN -Using Ranges吗?

Range("AD:AD").Select
Range("AE:AE").Select

答案 1 :(得分:0)

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

.Evaluate("=SUMPRODUCT((Column1=Source1_Val)*(Column2=Source2_Val)*ROW())")

答案 2 :(得分: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