繁体   English   中英

使用VBA如何将CountIfs与变量一起使用

[英]With VBA how to use CountIfs with a variable

我无法在将CountIfs与变量一起使用的任何地方找到示例。 为什么这会产生“对象已重置”错误?

Dim recTable As ListObject
Dim EOM As Date
Dim Pending As Double

For x = 1 To RecordCount
    If Not IsNull(recTable.DataBodyRange(x, 7).Value) Then
                    Pending = Pending + WorksheetFunction.CountIfs(recTable.DataBodyRange(x, 2).Value, "<=" & EOM, recTable.DataBodyRange(x, 7).Value, ">" & EOM)
                ElseIf IsNull(recTable.DataBodyRange(x, 7).Value) And Not IsNull(recTable.DataBodyRange(x, 6).Value) Then
                    Pending = Pending + Application.WorksheetFunction.CountIfs(recTable.DataBodyRange(x, 2).Value, "" <= "" & EOM, recTable.DataBodyRange(x, 6).Value, "" > "" & EOM)
                Else
                    Pending = Pending + 1
            End If
        Debug.Print Pending
Next x

根据评论,我建议以下内容:

Dim recTable As ListObject
Dim EOM As Date
Dim Pending As Double ' Maybe Long or Integer?

'recTable is not set in the posted code, but I assume it is in the actual code
'EOM is not set in the posted code, but I assume it is in the actual code
'RecordCount is not declared or set in the posted code, but I assume it is in the actual code
'x is not declared in the posted code, but I assume it is in the actual code

With recTable
    For x = 1 To RecordCount
        If Not IsNull(.DataBodyRange(x, 7).Value) Then
            If .DataBodyRange(x, 2).Value <= EOM And _
               .DataBodyRange(x, 7).Value > EOM Then
                Pending = Pending + 1
            End If
        ElseIf Not IsNull(.DataBodyRange(x, 6).Value) Then
            If .DataBodyRange(x, 2).Value <= EOM And _
               .DataBodyRange(x, 6).Value > EOM The
                Pending = Pending + 1
            End If
        Else
            Pending = Pending + 1
        End If
        Debug.Print Pending
    Next x
End With

暂无
暂无

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

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