Excel超实用技能,实现按单元格颜色求和与计数,千万不要错过( 三 )

步骤一:首先需要启用Visual Basic编辑器 , 右键单击示例工作表 , 然后左键单击【查看代码】选项 。 在Visual Basic编辑器中的工程资源管理器窗口中 , 右键单击后将鼠标指针移动至插入选项 , 然后插入一个模块 , 并将以下代码复制粘贴到代码窗口中 , 最后关闭Visual Basic编辑器回到Excel工作表操作界面 。

代码区域:

Function CountColor(col As Range countrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In countrange
If icell.Interior.ColorIndex = col.Interior.ColorIndex Then
CountColor = CountColor + 1
End If
Next icell
End Function
Function SumColor(col As Range sumrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In sumrange
If icell.Interior.ColorIndex = col.Interior.ColorIndex Then
SumColor = Application.Sum(icell) + SumColor

推荐阅读