(条件)在SUMPRODUCT WorksheetFunction中

时间:2015-08-23 09:14:32

标签: excel vba excel-vba worksheet-function

实际上,我需要使用Variant Arrays运行COUNTIFS WorksheetFunction,但猜测这在VBA中是不可能的。另一种方法是在VBA中编写我自己的COUNTIFS函数,但我想知道是否可以在VBA中编写以下SUMPRODUCT函数...

=SUMPRODUCT(--(Table1[Col1]="Something"),--(Table1[Col2]="Something"))

如果我们能够做到这一点,那么我就不需要编写我的额外功能了。不知道哪个会更快。

1 个答案:

答案 0 :(得分:1)

如果您只是在寻找使用结构化表引用的操作$unwind,那么$geoNear及其属性就可以了。

Sub dermal()
    Dim hdr1 As Long, hdr2 As Long, cntif As Long
    Dim str As String, app As Application

    Set app = Application

    With Worksheets("Sheet1").ListObjects("Table1")
        hdr1 = app.Match("Col1", .HeaderRowRange.Rows(1), 0)
        hdr2 = app.Match("Col2", .HeaderRowRange.Rows(1), 0)

        str = "something"
        cntif = app.CountIfs( _
            .ListColumns(hdr1).DataBodyRange, str, _
            .ListColumns(hdr2).DataBodyRange, str)
        Debug.Print cntif

    End With

    Set app = Nothing
End Sub

较新的COUNTIFS比同等COUNTIFS function更快;通常占用30%的时间来完成等效操作,即使在使用全列范围差异时,SUMPRODUCT已被削减到数据的最大范围。

如果您绝对需要挤出每毫秒并且需要使用内存处理的变体数组,那么我建议使用ListObject object SUMPRODUCT function中的过滤方法来构建类结构。