计算具有特定填充颜色和单元格值的单元格数量

我正在使用下面的代码,但它不工作。

Function CountColorValue(CountRange As Range, CountColor As Range, CountVAlue As Range) Dim numbers As Long Dim Cell As Range Set rCell = CountRange For Each rCell In CountRange If rCell.Interior.ColorIndex = Color Then If rCell.Value = Value Then numbers = numbers + 1 End If GetColorCount = TotalCount End Function 

虚心build议你进入VBE的工具►选项,并在编辑器属性页上,勾上需要variables声明旁边的复选标记。 这将帮助您避免拼写错误或完全忽略您使用的variables的名称。

 Function CountColorValue(CountRange As Range, CountColor As Range, CountValue As Range) Dim iNumbers As Long Dim rCell As Range For Each rCell In CountRange If rCell.Interior.ColorIndex = CountColor.Interior.ColorIndex Then If rCell.Value2 = CountValue.Value2 Then iNumbers = iNumbers + 1 End If End If Next rCell CountColorValue = iNumbers End Function 

它看起来像你试图把两个密切相关的函数混合在一起,但是如果没有正确的variables声明和赋值,你就不能使用另一个的一部分。