简体   繁体   English

SUMPRODUCT WorksheetFunction中的(条件)

[英](Condition) in SUMPRODUCT WorksheetFunction

In reality, I need to run a COUNTIFS WorksheetFunction using Variant Arrays, but guess this is not possible in VBA. 实际上,我需要使用Variant Arrays运行COUNTIFS WorksheetFunction,但是猜测这在VBA中是不可能的。 The alternative would be to write my own COUNTIFS function in VBA, but I was wondering whether it'd be possible to write the following SUMPRODUCT function in VBA... 另一种选择是在VBA中编写自己的COUNTIFS函数,但是我想知道是否可以在VBA中编写以下SUMPRODUCT函数...

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

If we can do that, then I'd not need to write my extra function. 如果我们能够做到这一点,那么我就不需要编写我的额外功能了。 Not sure which would be faster though. 不确定哪个会更快。

If you are just looking for an operational COUNTIFS function using structured table references then the ListObject object and its properties is the way to go. 如果您只是在寻找使用结构化表引用的可操作COUNTIFS函数,那么ListObject对象及其属性就是您的最佳选择。

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

The newer COUNTIFS is much quicker that a comparable SUMPRODUCT function ; 较新的COUNTIFS比起类似的SUMPRODUCT函数要快得多; typically taking 30% of the time to complete equivalent operations even when used with full column range erferences while SUMPRODUCT's have been trimmed down to the barest extents of the data. 即使将SUMPRODUCT缩小到数据的最小范围,即使与完整的列范围误差一起使用,通常也要花费30%的时间来完成等效操作。

If you absolutely need to squeeze out every milli-second and require variant arrays with in-memory processing then I would recommend a class structure using the filtering methods in Filtering 2D Arrays in Excel VBA by assylias . 如果您绝对需要每毫秒挤出一次并且需要具有内存处理功能的变体数组,那么我建议使用assylias的“ 在Excel VBA过滤2D数组”中的过滤方法使用类结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM