提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何根据Excel中的字体颜色计算/求和单元格?

DOC-计数字体color1

你怎么能得到单元格的数量或总和Excel中包含特定字体颜色的所有单元格? 例如,我在工作表中有一系列数据,如下面的截图所示,现在我要分别计算或求和具有红色,蓝色和黑色字体颜色的单元格。在Excel中,没有直接的公式可以处理这个任务,在这里,我会谈谈一些搞定这个工作的技巧。

使用用户定义函数基于字体颜色计算或求和单元格

使用Kutools for Excel对基于字体颜色的单元格进行计数或求和

使用Kutools for Excel对基于背景颜色的单元格进行计数或求和


基于背景,字体,条件格式化颜色计数/求和单元格值:

Kutools for Excel's 按颜色计数 功能可以帮助您根据您的需要,根据字体,背景或条件格式化颜色进行一些计算。

doc过滤器颜色12

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!


箭头蓝色右泡 使用用户定义函数基于字体颜色计算或求和单元格


根据字体颜色计算单元格:

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

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

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

VBA代码:根据字体颜色计算单元格:

Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
'Update 20140210
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:D8,A1) ,看截图:

DOC-计数字体color1

注意:在上面的公式中, A1:D8 是你想要使用的范围 A1 是您要计算的具有特定字体颜色的单元格。

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

DOC-计数字体color1

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

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

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

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

VBA代码:基于字体颜色的总和单元格:

Public Function SumByColor(pRange1 As Range, pRange2 As Range) As Double
'Update 20140210
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) 进入一个空白单元格,看截图:

DOC-计数字体color1

注意:在上面的公式中, A1:D8 是你想要使用的范围 A1 是具有特定字体颜色的单元格,您要进行求和。

4。 然后按 输入 关键,你会把所有单元格加上红色的字体颜色。 如果你想总结其他字体颜色的单元格,请重复输入公式。 你会得到以下结果:

DOC-计数字体color1


箭头蓝色右泡 使用Kutools for Excel对基于字体颜色的单元格进行计数或求和

用户定义函数可能是你保存和应用的麻烦,在这里,我会推荐你​​一个方便的, Kutools for Excel,其 按颜色计数 实用程序,您可以通过背景颜色,字体颜色,条件格式快速获得计算结果,如计数,总和,平均单元格等。

Kutools for Excel : 与超过300方便的Excel加载项,在60天免费试用没有限制.

安装后 Kutools for Excel请按照以下步骤进行:

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

2。 点击 企业服务 > 按颜色计数,看截图:

DOC-计数字体color1

3。 在 按颜色计数 对话框中选择 标准格式 来自 色彩方法 下拉,然后选择 字体 在下面 计数类型 下拉,看截图:

DOC-计数字体color1

4。 统计字体颜色相同的单元格进行统计,合计,平均等。 然后点击 生成报告 按钮,您将在新的工作簿中获得统计信息。 看截图:

DOC-计数字体color1

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


箭头蓝色右泡 使用Kutools for Excel对基于背景颜色的单元格进行计数或求和

如果要计算或总结具有特定背景颜色的单元格, Kutools for Excel按颜色计数 功能也可以帮助你,请做如下:

1。 根据背景颜色选择要计数或求和的单元格的数据范围,然后单击以启用该功能 企业服务 > 按颜色计数.

2.按颜色计数 对话框中选择 标准格式 来自 色彩方法 下拉,然后选择 背景 在下面 计数类型 下拉,看截图:

DOC-计数字体color1

3。 然后点击 生成报告 按钮,基于背景颜色生成报告,请参见截图:

DOC-计数字体color1

点击了解更多关于这个计数颜色功能。

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


箭头蓝色右泡 根据字体/背景/条件格式化颜色计算或求和单元格:

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!


Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能30天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    me · 1 months ago
    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?
  • To post as a guest, your comment is unpublished.
    Sarah · 4 months ago
    This was exactly what I needed - Thanks!
  • To post as a guest, your comment is unpublished.
    ISMERCIO · 4 months ago
    = 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

  • To post as a guest, your comment is unpublished.
    Borja · 5 months ago
    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
    • To post as a guest, your comment is unpublished.
      skyyang · 4 months ago
      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!
  • To post as a guest, your comment is unpublished.
    Xiahui Tan · 7 months ago
    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
    • To post as a guest, your comment is unpublished.
      skyyang · 7 months ago
      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:
  • To post as a guest, your comment is unpublished.
    Kriss · 7 months ago
    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?
    • To post as a guest, your comment is unpublished.
      skyyang · 7 months ago
      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
  • To post as a guest, your comment is unpublished.
    Tahj · 8 months ago
    Exactly what I was looking for! This will save me lots of time. Thank You
  • To post as a guest, your comment is unpublished.
    Fernanda · 9 months ago
    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 - webchio@gmail.com
  • To post as a guest, your comment is unpublished.
    Cheryl · 10 months ago
    This was SO helpful - Thanks very much!
  • To post as a guest, your comment is unpublished.
    Hien Dang · 1 years ago
    Hướng dẫn bên trên hình như chỉ áp dụng khi màu của giá trị trong ô hoặc màu nền của ô được tô màu bằng Manual.M muốn đếm số ô mà giá trị được bôi màu theo cách Conditional Formatting.Xin giúp đỡ ạ!
  • To post as a guest, your comment is unpublished.
    Gennelee · 1 years ago
    Hi I tried it but when I changed the color of other items, it doesnt add up or the sum doesnt change.
    Please help me.
  • To post as a guest, your comment is unpublished.
    JWong · 1 years ago
    Hi,

    I followed the instructions and it works well. But then when i re-open the file, the module window is empty. It seems i cannot save the code as needed. A box pops up saying that "VB project cannot be saved in macro-free workbooks" and therefore i have to save as a macro-free workbook.

    Please advise if I have missed any steps.

    Thanks,
    J. Wong
  • To post as a guest, your comment is unpublished.
    Adam · 1 years ago
    Rather than count or sum cells based on font colour, is there a way to display certain 'values' in another column based on font colour? for example, I have a column with multiple company names that each have a specific font colour to indicate a specific calendar year. I would like to simply create a 'year' column beside this. So for example if column B contains text with font colour green, this will display "2017" in column C. And if column B contains text with font colour red, this will display "2016" in column C. I dread to do this manually as I have thousands of rows!
  • To post as a guest, your comment is unpublished.
    antonella · 1 years ago
    Hi,
    I followed the instructions above for SumByColor and they worked great.
    I was wondering whether there is a similar formula in VB to sum by background color rather than using Kutools.
    thanks, Antonella
  • To post as a guest, your comment is unpublished.
    Mike · 2 years ago
    hi, your solution works well but when i change the content of a cell in the range to the colour selected the result cell doesn't auto update unless i go to the result cell select the formula and hit enter. Is there a way to auto update the result cell when the range is amended.
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      Hello, Mike,
      May be there is no other good method to update the result automatically when you change the cell font color, so, you need selec t the formula cell and hit Enter key or click the Automatic under the Formulas tab to get the updated result.
      If you have any other good ways, please comment here.
      Thank you very much!
  • To post as a guest, your comment is unpublished.
    mfleming · 2 years ago
    I did everything as instructed by I get #VALUE! instead of a total. I changed the formula to subtotal one single cell and it worked but when the formula has a range of cells (in my case D3:D53, I get the #VALUE result.
  • To post as a guest, your comment is unpublished.
    dfmoeller · 2 years ago
    I can't seem to get past this syntax error that I am getting. I'm not very knowledgeable when it comes to visual basic, so I'm hoping that it is just a small typing error (although I did cut and paste the code from above, rather than retype). The machine is running Windows 10/Office 2013. The error says "compile error / syntax error". The VB windows shows the line with the IF statement in the SumByColor module as highlighted. Please tell me what I'm doing wrong.
  • To post as a guest, your comment is unpublished.
    Liz · 2 years ago
    I followed the instructions for SUMbyColor (for font).Showed up perfectly on my screen (my work pc has Excel 2010 ) however, on my boss's screen it comes up as '#NAME?' (fyi, his pc has the current Excel version).Is there any way to resolve this?
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      [quote name="Liz"]I followed the instructions for SUMbyColor (for font).Showed up perfectly on my screen (my work pc has Excel 2010 ) however, on my boss's screen it comes up as '#NAME?' (fyi, his pc has the current Excel version).Is there any way to resolve this?[/quote]
      Hello,LIZ:
      After inserting the code, you should save the workbook as [b]Excel Macro-Enabled Workbook[/b] file format. And then when you open this Excel file next time, you should click[b] Enable Content[/b] from the security warning ribbon to activate the macro code. Please try this.
  • To post as a guest, your comment is unpublished.
    Luke · 2 years ago
    Hi tks for the advice.
    I have a similar problem of MYTAWALBEH
    I would like to include more columns in the function
    would be great if someone has some tips
    cheers
  • To post as a guest, your comment is unpublished.
    Smudge · 2 years ago
    Great read and working brilliant however is it possible for anyone to explain how I could apply this to a multiple set of sheets??

    Any idea will be much appreciated so cheers in advance.
  • To post as a guest, your comment is unpublished.
    Smudge · 2 years ago
    Great read and works awesome but could someone please tell me how to apply this to multiple sheets.

    If anyone can, this will be brilliant for what I need and will be much appreciated indeed.
  • To post as a guest, your comment is unpublished.
    Phillip · 2 years ago
    Is there any way to count the cells by colour without Kutools?
  • To post as a guest, your comment is unpublished.
    Nicola · 3 years ago
    Hi! Works great, thanks.

    However, doesnt work for cells with more than one colour. Is it possible to update this code to do this?

    Nikki
  • To post as a guest, your comment is unpublished.
    KH · 3 years ago
    How do I get the spread sheet to automatically update? If my data/color changes the sum doesn't update until I select the cell with the formula, hit F2, and then click off the cell. I want my sheet to automatically update as the data changes. Thanks.
  • To post as a guest, your comment is unpublished.
    atul gaur · 3 years ago
    Sir#name error aa rahi hai
  • To post as a guest, your comment is unpublished.
    Leanne · 3 years ago
    Hi

    I need to count the number of cells in orange font from cell range B:B if Cell Range AB says 'Ventura'

    The above allows you to count according to one criteria but doesn't allow a second - I have tried but I have never written macros before and cannot get this working. Urgent help needed please?

    Thank you
    Leanne
  • To post as a guest, your comment is unpublished.
    Diana · 4 years ago
    I followed your directions for the count but in the cells that I have =CountColour(...) I have the #NAME error. I have multiple tabs, so I put the code in the sheet where I want to use it but I still got the error, so then I put it in a module. What am I missing??
    • To post as a guest, your comment is unpublished.
      nada · 3 years ago
      Click the Microsoft Office Button , and then click Excel Options.
      Click Trust Center, click Trust Center Settings, and then click Macro Settings.
      Click the options to enable macro
  • To post as a guest, your comment is unpublished.
    gwenn · 4 years ago
    thank you very much... needed a little debugging but I think that's because nothing is ever cookie cutter, what I did to fix my issues may be different than what anyone else may need to do (based on version, etc.) but once I took out the rem'd line and made sure that I was pointing to the right fields and format field to do the comparison to, it worked like a charm.
  • To post as a guest, your comment is unpublished.
    Janna · 4 years ago
    the countcolor function includes the spaces between the cell that has that specific color. can you fix this?

    thanks alot!
  • To post as a guest, your comment is unpublished.
    magician1314 · 4 years ago
    MYTawalbeh,

    I had the same issue. You have to save the workbook as a macro enabled workbook so the macro copies over. Otherwise you will continue to have the error because the macro isn't being copied over.
  • To post as a guest, your comment is unpublished.
    MYTawalbeh · 4 years ago
    IT worked ,, thank you
    But when I save and close worksheet then re-open, it shows (#Name?)
    I've saved it (Macro-enabled)
    • To post as a guest, your comment is unpublished.
      Lisa · 2 years ago
      Click to enable macros on the yellow bar at the top of the page when you open your document and your formulas will come back. Hope this helps.
  • To post as a guest, your comment is unpublished.
    Stefanos N · 4 years ago
    Excellent work!

    Saved my wife's night!

    Thx
  • To post as a guest, your comment is unpublished.
    MEREDITH · 4 years ago
    Hello, I keep getting an error as follows "Ambiguous name detected: SumByColor". What am i doing wrong?
    • To post as a guest, your comment is unpublished.
      Lisa · 2 years ago
      I got this message when I tried to apply the same thing to another spreadsheet. When you go to insert module and copy in the text change it to SumByColour (different spelling and note there are two instances of it in the text). Then when I tried to use the formula in my second spreadsheet the new spelling came up which I selected and now the function works in this spreadsheet too. I hope this works for you because I am finding it extremely useful for various spreadsheets I work with.
  • To post as a guest, your comment is unpublished.
    Eddie L · 4 years ago
    I use a very similar method but have been playing with MULTI Colored fonts... say [Only the [b]BOLD[/b] word is red], fingers crossed you have a thought as to how I may achieve this!

    Cheers
    Eddie
  • To post as a guest, your comment is unpublished.
    nomnom · 4 years ago
    super cool, helped me a ton.

    if you don't mind me asking.
    when i try this on a colored cell number affected with "Conditional Formatting" it seems to see all the numbers as a single color. are there any way to have the program recognize that the color is different.
  • To post as a guest, your comment is unpublished.
    Khizar Hayat. · 5 years ago
    it works very good and so nice function.

    thank you.
  • To post as a guest, your comment is unpublished.
    Oguz Canturk · 5 years ago
    How do we do well to access?
  • To post as a guest, your comment is unpublished.
    Nick Roma · 5 years ago
    Hi,
    when i use this formula it keeps coming up with "Compile Error: Must be first statement on the line" and it has highlighted the "Function" on the first line of the formula. Help??
    • To post as a guest, your comment is unpublished.
      Suzy · 2 years ago
      May be you did not insert a module instead copied the code on the screen after Alt+F11
  • To post as a guest, your comment is unpublished.
    Minh Chau · 5 years ago
    Thank you so much! It works like wonder!