【问题标题】:CountIf and xlCellTypeVisible producing 1004 errorCountIf 和 xlCellTypeVisible 产生 1004 错误
【发布时间】:2014-08-08 23:27:03
【问题描述】:

我正在尝试计算包含“更改”一词的字段的数量,但这是在过滤列表上执行的,因此我使用 SpecialCells(xlCellTypeVisible) 属性来测试它们是否是过滤的一部分列表。示例代码如下所示:

aWorkbook.Worksheets("Sheet1").Cells(22, 10).Formula = WorksheetFunction.CountIf(Range(Cells(3, 10), Cells(20, 10)).SpecialCells(xlCellTypeVisible), "Change")

但我收到以下错误:

Run-time error'1004': Unable to get the CountIf property of the WorksheetFunction class

以下使用 Sum 的代码按预期工作,但我觉得很奇怪

aWorkbook.Worksheets("Sheet1").Cells(22, 7).Formula = WorksheetFunction.Sum(Range(Cells(3, 7), Cells(20, 7)).SpecialCells(xlCellTypeVisible))

【问题讨论】:

    标签: vba excel


    【解决方案1】:
    Public Sub CountValue()
    Dim rngValues as Range
    Dim varCounter as Variant
    Dim counter as Byte
    
    Set rngvalues = ThisWorkbook.Sheets("Sheet1").Range("$J$3:$J$20")
    
    For Each varCounter in rngvalues.SpecialCells(xlCellTypeVisible)
        If varCounter = "Change" Then
           counter = counter + 1
        End if
    Next
    
    ThisWorkbook.Sheets("1").Range("$J$22").Value = counter
    
    End Sub
    

    【讨论】:

    • 这是我的下一个方法,但有谁知道为什么上述方法不起作用?
    • 你不应该在没有评估的情况下在 WorksheetFunctions 中混合 VBA 函数
    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2023-03-29
    • 1970-01-01
    • 2023-01-26
    • 2017-11-27
    相关资源
    最近更新 更多