跳到主要内容

如何根据Excel中的字体颜色对单元格进行计数或求和?

如何在Excel中获取单元格的数量或将包含特定字体颜色的所有单元格求和? 例如,我在工作表中有一系列数据,如下面的屏幕快照所示,现在我想分别计算或求和具有红色,蓝色和黑色字体颜色的单元格。 通常,没有直接的方法可以处理此任务,在这里,我将讨论解决此任务的一些技巧。

使用用户定义的功能根据字体颜色对单元格进行计数或求和

使用一些有用的功能根据字体颜色对单元格进行计数或求和

根据字体颜色对单元格进行计数或求和,具有惊人的功能-按颜色计数


使用用户定义的功能根据字体颜色对单元格进行计数或求和

根据字体颜色对单元格进行计数:

要计算具有特定字体颜色的单元格数量,请执行以下操作:

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:根据字体颜色对单元格进行计数:

Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
'Update by Extendoffice
Application.Volatile
Dim rng As Range
For Each rng In pRange1
    If rng.Font.Color = pRange2.Font.Color Then
        CountColour = CountColour + 1
    End If
Next
End Function

3。 然后保存此代码并返回到工作表,然后在空白单元格中输入此公式 = CountColour(A1:D10,A2) ,请参见屏幕截图:

备注:在以上公式中, A1:D10 是您要使用的范围, A2 是要计数的具有特定字体颜色的单元格。

4。 输入公式后,按 输入 键,您将获得带有红色字体颜色的单元格数。 如果要计算其他字体颜色的单元格,请根据需要重复输入公式。 您将得到以下结果:


根据字体颜色对单元格求和:

要根据字体颜色对单元格求和,以下用户定义函数可以为您提供帮助。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:根据字体颜色对单元格求和:

Public Function SumByColor(pRange1 As Range, pRange2 As Range) As Double
'Update by Extendoffice
Application.Volatile
Dim rng As Range
Dim xTotal As Double
xTotal = 0
For Each rng In pRange1
    If rng.Font.Color = pRange2.Font.Color Then
        xTotal = xTotal + rng.Value
    End If
Next
SumByColor = xTotal
End Function

3。 然后保存此代码并返回到原始工作表,然后输入此公式 = SumByColor(A1:D8,A1) 放入空白单元格,请参见屏幕截图:

备注:在以上公式中, A1:D10 是您要使用的范围, A2 是您要求和的具有特定字体颜色的单元格。

4。 然后按 输入 键,您将以红色字体颜色添加所有单元格。 如果要对其他字体彩色单元格求和,请重复输入公式。 您将得到以下结果:


使用一些有用的功能根据字体颜色对单元格进行计数或求和

可能是用户定义函数对您的保存和应用造成麻烦,在这里,我将向您推荐一个方便的工具-Kutools for Excel,凭借其先进的功能,您可以快速轻松地解决此任务。

提示:申请这个 COUNTBY字体颜色字体颜色 功能,首先,您应该下载 Kutools for Excel,然后快速轻松地应用该功能。

安装后 Kutools for Excel,请这样做:

根据字体颜色对单元格进行计数:

1。 单击要放入计数结果的单元格,然后单击 库工具 > Kutools函数 > 统计与数学 > COUNTBY字体颜色,请参见屏幕截图:

2。 在 功能参数 对话框中,指定要按字体颜色计数的数据范围和颜色索引单元,请参见屏幕截图:

3。 然后,单击 OK 按钮,您将获得第一个计数结果,要获得其他结果,只需复制此公式并根据需要更改单元格引用即可。 看截图:


根据字体颜色对单元格求和:

1。 单击要放入计数结果的单元格,然后单击 库工具 > Kutools函数 > 统计与数学 > 字体颜色,请参见屏幕截图:

2。 在 功能参数 对话框中,指定要按字体颜色求和的数据范围和颜色索引单元,请参见屏幕截图:

3。 然后,单击 OK 按钮,您将获得第一个总和结果,要获得其他结果,只需复制此公式并更改所需的单元格引用即可。 看截图:

点击下载Kutools for Excel并立即免费试用!


根据字体颜色对单元格进行计数或求和,具有惊人的功能-按颜色计数

Kutools for Excel 还提供了一个简单的功能- 按颜色计数,使用此实用程序,您可以根据需要通过背景色,字体颜色和条件格式快速获得计算结果,例如计数,总和,平均单元格等。

提示:申请这个 按颜色计数 功能,首先,您应该下载 Kutools for Excel,然后快速轻松地应用该功能。

安装后 Kutools for Excel,请这样做:

1。 根据不同的颜色选择要计数和求和的数据范围。

2。 点击 Kutools 加> 按颜色计数,请参见屏幕截图:

3。 在 按颜色计数 对话框,选择 标准格式 来自 上色方式 下拉菜单,然后选择 字体 在下面 计数类型 下拉,并且已经对具有相同字体颜色的单元格进行了计数,累加,平均等操作,请参见屏幕截图:

4。 然后点击 生成报告 按钮,您将在新工作簿中获得统计信息。 看截图:

点击下载Kutools for Excel并立即免费试用!


更多文章:

  • 在Excel中基于背景色对单元格进行计数和求和
  • 假设您有一系列具有不同背景颜色的单元格,例如红色,绿色,蓝色等,但是现在您需要计算该范围内有特定背景颜色的单元格的数量,并对具有相同特定颜色的彩色单元格求和。 在Excel中,没有直接公式可以计算颜色单元的总数和计数,这里我将向您介绍一些解决此问题的方法。
  • 在Excel中的单元格范围内加总/计数粗体数字
  • 如果您的数据范围包含工作表中的一些粗体数字,而现在您只想对粗体单元格进行求和或计数,那么您当然可以手动将它们一一加起来,但这会很费时。 您如何以一种简便快捷的方式对Excel中的粗体单元格求和或计数?
  • 在多个单元格上应用颜色渐变
  • 在Excel中,我们可以轻松地为一个单元格或多个单元格填充背景色,但是有时,我们需要按照下面的截图所示填充渐变色,如何在Excel中获得一个单元格或多个单元格中的颜色渐变?
  • 连接单元格列并在Excel中保留文本颜色
  • 众所周知,将单元格列连接或合并为一列时,单元格格式(例如文本字体颜色,数字格式等)将丢失。 本文,我将介绍一些技巧,以将单元格列合并为一个,并在Excel中尽可能轻松地保持文本颜色。

根据字体/背景/条件格式颜色对单元格进行计数或求和:

Kutools for Excel:具有300多个方便的Excel加载项,可以在30天内免费试用,没有任何限制。 立即下载并免费试用!

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (52)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi..


I used it to count and sum from matrix. The problem is that the I need to count/sum in multiple range of cells. Is it possible to update this code to do this?
This comment was minimized by the moderator on the site
Hello, Joseph,

Maybe there is no direct way for you to count or sum the cell values based on font color in multiple ranges, but, you can apply the third method in this article and use Count by Color feature of Kutools for Excel, with this feature, you just need to select the multiple ranges first, and then apply the feature.
https://www.extendoffice.com/documents/excel/1418-excel-count-sum-by-font-color.html#a3

Please try it, thank you!
This comment was minimized by the moderator on the site
COUNT CELLS BY FONT COLOR tutorial was great! But it is not working when the font color was based on the conditional formatting. Do you have something for this concern?
This comment was minimized by the moderator on the site
Hello,
The codes in this article can not support the conditional formatting, but, you can use our Kutools for Excel's Count by Color feature, it can help you to count or sum conditional font colors. Please try it.
This comment was minimized by the moderator on the site
This was exactly what I needed - Thanks!
This comment was minimized by the moderator on the site
= SumByColor (A1: D8, A1) NÃO FUNCIONOU


=SumByColor(A1:D8;A1) FUNCIONOU

TIRANDO ESPAÇO E USANDO " ; " AO INVÉS DE " , " AI FUNCIONOU LEGAL.

USO EXCEL 10


MUITO BOM.


OBRIGADO
This comment was minimized by the moderator on the site
Is there a way to change Count Or Sum Cells Based On Font Colors With User Defined Function into count or sum cells based on de conditional formating?

I've tried with

Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double

Application.Volatile
Dim rng As Range
For Each rng In pRange1

If rng.FormatConditions.Font.Color = pRange2.FormatConditions.Font.Color Then
CountColour = CountColour + 1

End If
Next
End Function

But it appears not to work, any suggestion?
Thanks
This comment was minimized by the moderator on the site
Hello, Borja,
May be you can use our product, Kutools for Excel, with its Count by Color feature, you can quickly count or sum the cell values based on the conditional formatting without any VBA code, please try. You can download it and free trial in 60 day.
Thank you!
This comment was minimized by the moderator on the site
I tried the SumByColor. It works, but not if the numbers have been coloured by conditional formatting. For example, I have a list of different numbers that are coloured red if they are within a range set by a conditional formatting rule. I would then like to sum only the red-coloured numbers. But the SumByColor VBA code does not work in this situation. Any suggestions to make it work. Thanks
This comment was minimized by the moderator on the site
Hello, Xiahui,
The above code can not applied to conditional formatting color, but, you can use our Kutools for Excel' Count by color feature, with it, you can quickly get the result for counting or summing based on the conditional formatting color. See the below screenshot:
This comment was minimized by the moderator on the site
Hey
Nice macro. I used it to just count from a simple matrix. The problem is that the macro counts also empty cells. How to exclude empty cells from counting?
This comment was minimized by the moderator on the site
Hello, Kriss,
To count the cells based on font color excluding the blank cells, please apply the below User Defined Function, please try:

Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
Application.Volatile
Dim rng As Range
For Each rng In pRange1
If rng.Value <> "" Then
If rng.Font.Color = pRange2.Font.Color Then
CountColour = CountColour + 1
End If
End If
Next
End Function
This comment was minimized by the moderator on the site
Exactly what I was looking for! This will save me lots of time. Thank You
This comment was minimized by the moderator on the site
Muito obrigada já ajudou muito, porém a minha formula só adiciona à soma outro número quando eu uso o pincel para mudar a cor, se eu trocar a cor da fonte pela barra de ferramenta não dá certo, alguém saber me explicar -
This comment was minimized by the moderator on the site
This was SO helpful - Thanks very much!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations