跳到主要內容

如何根據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。 輸入公式後,按 Enter 鍵,您將獲得帶有紅色字體顏色的單元格數。 如果要計算其他字體顏色的單元格,請根據需要重複輸入公式。 您將得到以下結果:


根據字體顏色對單元格求和:

要根據字體顏色對單元格求和,以下用戶定義函數可以為您提供幫助。

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。 然後按 Enter 鍵,您將以紅色字體顏色添加所有單元格。 如果要對其他字體彩色單元格求和,請重複輸入公式。 您將得到以下結果:


使用一些有用的功能根據字體顏色對單元格進行計數或求和

可能是用戶定義函數對您的保存和應用造成麻煩,在這裡,我將向您推荐一個方便的工具-Excel的Kutools,憑藉其先進的功能,您可以快速輕鬆地解決此任務。

提示:要應用此 COUNTBY字體顏色字體顏色 功能,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請這樣做:

根據字體顏色對單元格進行計數:

1。 單擊要放入計數結果的單元格,然後單擊 庫工具 > Kutools函數 > 統計與數學 > COUNTBY字體顏色,請參見屏幕截圖:

2。 在 功能參數 對話框中,指定要按字體顏色計數的數據范圍和顏色索引單元,請參見屏幕截圖:

3。 然後,單擊 OK 按鈕,您將獲得第一個計數結果,要獲得其他結果,只需複制此公式並根據需要更改單元格引用即可。 看截圖:


根據字體顏色對單元格求和:

1。 單擊要放入計數結果的單元格,然後單擊 庫工具 > Kutools函數 > 統計與數學 > 字體顏色,請參見屏幕截圖:

2。 在 功能參數 對話框中,指定要按字體顏色求和的數據范圍和顏色索引單元,請參見屏幕截圖:

3。 然後,單擊 OK 按鈕,您將獲得第一個總和結果,要獲得其他結果,只需複制此公式並更改所需的單元格引用即可。 看截圖:

點擊下載Kutools for Excel並立即免費試用!


根據字體顏色對單元格進行計數或求和,具有驚人的功能-按顏色計數

Excel的Kutools 還提供了一個簡單的功能- 按顏色計數,使用此實用程序,您可以根據需要通過背景顏色,字體顏色和條件格式快速獲得計算結果,例如計數,總和,平均單元格等。

提示:要應用此 按顏色計數 功能,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請這樣做:

1。 根據不同的顏色選擇要計數和求和的數據范圍。

2。 點擊 Kutools 加> 按顏色計數,請參見屏幕截圖:

3。 在 按顏色計數 對話框中選擇 標準格式 來自 上色方式 下拉菜單,然後選擇 字體計數類型 下拉,並且已經對具有相同字體顏色的單元格進行了計數,累加,平均等操作,請參見屏幕截圖:

4。 然後點擊 生成報告 按鈕,您將在新工作簿中獲得統計信息。 看截圖:

點擊下載Kutools for Excel並立即免費試用!


更多文章:

  • 在Excel中基於背景色對單元格進行計數和求和
  • 假設您有一系列具有不同背景顏色的單元格,例如紅色,綠色,藍色等,但是現在您需要計算該範圍內有特定背景顏色的單元格的數量,並對具有相同特定顏色的彩色單元格求和。 在Excel中,沒有直接公式可以計算顏色單元的總數和計數,這裡我將向您介紹一些解決此問題的方法。
  • 在Excel中的單元格範圍內加總/計數粗體數字
  • 如果您的數據范圍包含工作表中的一些粗體數字,而現在您只想對粗體單元格進行求和或計數,那麼您當然可以手動將它們一一加起來,但這會很費時。 您如何以一種簡便快捷的方式對Excel中的粗體單元格求和或計數?
  • 在多個單元格上應用顏色漸變
  • 在Excel中,我們可以輕鬆地為一個單元格或多個單元格填充背景色,但是有時,我們需要按照下面的截圖所示填充漸變色,如何在Excel中獲得一個單元格或多個單元格中的顏色漸變?
  • 連接單元格列並在Excel中保留文本顏色
  • 眾所周知,將單元格列連接或組合為一列時,單元格格式(例如文本字體顏色,數字格式等)將丟失。 本文,我將介紹一些技巧,以將單元格列合併為一個,並在Excel中盡可能輕鬆地保持文本顏色。

根據字體/背景/條件格式顏色對單元格進行計數或求和:

Excel的Kutools:具有300多個方便的Excel加載項,可以在30天內免費試用,沒有任何限制。 立即下載並免費試用!

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為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