ColinOL

A Small Website

用户工具

站点工具


wiki:app:office-excel-count-color-cell

Excel 实现按照单元格和字体颜色计数(Count)与求和(Sum)

步骤一

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

代码如下:

script.vb
'计算获得与引用单元格相同底纹颜色的单元格的数量
Function GetCountColorBack(col As Range, countbackrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In countbackrange
If icell.Interior.ColorIndex = col.Interior.ColorIndex Then
GetCountColorBack = GetCountColorBack + 1
End If
Next icell
End Function
 
'计算获得与引用单元格相同底纹颜色的单元格的数值和
Function GetSumColorBack(col As Range, sumbackrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In sumbackrange
If icell.Interior.ColorIndex = col.Interior.ColorIndex Then
GetSumColorBack = Application.Sum(icell) + GetSumColorBack
End If
Next icell
End Function
 
'计算获得与引用单元格相同文字颜色的单元格的数量
Function GetCountColorFont(col As Range, countfontrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In countfontrange
If icell.Font.ColorIndex = col.Font.ColorIndex Then
GetCountColorFont = GetCountColorFont + 1
End If
Next icell
End Function
 
'计算获得与引用单元格相同文字颜色的单元格的数值和
Function GetSumColorFont(col As Range, sumfontrange As Range) As Integer
Dim icell As Range
Application.Volatile
For Each icell In sumfontrange
If icell.Font.ColorIndex = col.Font.ColorIndex Then
GetSumColorFont = Application.Sum(icell) + GetSumColorFont
End If
Next icell
End Function

步骤二

求相同颜色的单元格数量,需要用到GetCountColorFont函数。函数GetCountColorFont的使用方法:=GetCountColorFont(指定颜色的单元格,统计单元格区域)

参考:

  1. Excel超实用技能,按单元格颜色求和与计数,千万不要错过 https://kuaibao.qq.com/s/20190814A0SZY000?refer=cp_1026
  2. 如何分别计数[Excel]不同颜色的单元格? https://www.zhihu.com/question/54651422
wiki/app/office-excel-count-color-cell.txt · 最后更改: 2021/07/22 08:31 由 colin