您现在的位置是:主页 > 单元格 >

excel按指定的单元格颜色进行计数或求和

发布时间:2023-10-29 11:57:20所属栏目:单元格 已帮助编辑作者:Excel自学教程

如果Excel工作表的某区域中包含不同的底纹颜色,我们可以用一个自定义函数对该区域按指定的单元格颜色进行计数或求和。方法是:

    1.按Alt+F11,打开VBA编辑器。

    2.单击菜单“插入→模块”,将插入名称为“模块1”的模块,在右侧的代码窗口中输入下列代码:

Function SumByColor(Ref_color As Range, Sum_range As Range)
Application.Volatile
Dim iCol As Integer
Dim rCell As Range
iCol = Ref_color.Interior.ColorIndex
For Each rCell In Sum_range
If iCol = rCell.Interior.ColorIndex Then
SumByColor = SumByColor + rCell.Value
End If
Next rCell
End Function


Function CountByColor(Ref_color As Range, CountRange As Range)
Application.Volatile
Dim iCol As Integer
Dim rCell As Range
iCol = Ref_color.Interior.ColorIndex
For Each rCell In CountRange
If iCol = rCell.Interior.ColorIndex Then
CountByColor = CountByColor + 1
End If
Next rCell
End Function

 

    上述两个自定义函数,一个是SumByColor,可以对区域按指定单元格的颜色求和。另一个是CountByColor,可以统计区域中某种颜色的个数。这两个自定义函数都有两个参数,前一个参数指定包含某种颜色的单元格,后一个参数为求和或计数区域。

    3.关闭VBA编辑器。

    使用方法:假如要求和或计数的区域在A1:B10区域中。

   

    求出该区域中单元格底纹颜色为红色的所有单元格数值之和,在单元格中输入公式:

    =sumByColor(A1,A1:B10)

    求出该区域中单元格底纹颜色为红色的所有单元格的个数,在单元格中输入公式:

    =CountByColor(A1,A1:B10)

     以上就是excel自学教程为您提供excel按指定的单元格颜色进行计数或求和的解读,本文章链接: http://www.5enet.cn/Excel_danyuange/76996.html 欢迎分享转载,更多相关资讯请前往单元格

站长名片

姓名:曾工

职业:IT运维

现居:深圳罗湖宝能大厦

电话:15768114603

扫码关注

  • 专注IT行业,10年IT行业经验
  • 微信