我正在尝试计算包含单词“Change”的字段数,但这是在筛选列表上执行的,所以我使用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))
答案 0 :(得分: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