Count the cells with specific Font color within the range of cell that have a specific background color

kalinajo

New Member
Joined
Jan 21, 2019
Messages
2
Hi all,

I want to know how much cell have a blue Font color within the cells that have a green background (and so on, 5 background colors and 5 Font colors).

Exemple: I have 20 cells that have a green background, from those cells how much have a blue Font?

Here are my formulas:

Background color count:

Function ColorCount(SearchRange As Range, colorRange As Range) As Long
Dim cell As Range, a As Range, b As Range, n As Integer

' preload for Union method (will Union with itself in first For loop)
Set b = SearchRange(1).MergeArea(1)

For Each cell In SearchRange
Set a = cell.MergeArea(1)
Set b = Union(a, b)
Next

' a becomes the preload for the next Union; n will be used to exclude
' it from the count if it's not the right color
n = a.Interior.Color = colorRange.Interior.Color

For Each cell In b
If cell.Interior.Color = colorRange.Interior.Color Then
Set a = Union(cell, a)
End If
Next

ColorCount = a.Count - 1 - n

End Function
-------------------------------------

*Please note that I need to use some merged cells in my file (no way out of it) and I need them to count as one cell. This Function seems to work perfectly for that.

Font Color count:

Function FontColorCount(SearchRange As Range, colorRange As Range) As Long
Dim cell As Range, a As Range, b As Range, n As Integer

' preload for Union method (will Union with itself in first For loop)
Set b = SearchRange(1).MergeArea(1)

For Each cell In SearchRange
Set a = cell.MergeArea(1)
Set b = Union(a, b)
Next

' a becomes the preload for the next Union; n will be used to exclude
' it from the count if it's not the right color
n = a.Font.Color = colorRange.Font.Color

For Each cell In b
If cell.Font.Color = colorRange.Font.Color Then
Set a = Union(cell, a)
End If
Next

FontColorCount = a.Count - 1 - n

End Function
---------------------------

I am stuck there! Help! I don't know how to use a function within an other one. Also would I need to use a Find function instead of a count Function ie: =Count Font color = Reference cell, IF(Background color = Reference cell) (Rough idea of what I want to do).

Thank you all for your support!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD][/TD]
[TD][/TD]
[TD="bgcolor: #92d050"]HI[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD][/TD]
[TD]B2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD][/TD]
[TD="bgcolor: #92d050"]B3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD][/TD]
[TD]B4[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD][/TD]
[TD]B5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD][/TD]
[TD="bgcolor: #92d050"]B6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD][/TD]
[TD="bgcolor: #92d050"]B7[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD][/TD]
[TD]B8[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Formula
CellFormula
D1=ColorInteriorFontCount(B2:B8,C1)

<tbody>
</tbody>

<tbody>
</tbody>



Code:
Function ColorInteriorFontCount(SearchRange As Range, colorRange As Range) As Long
    Dim cell As Range, a As Range, b As Range, n As Integer
    
    ' preload for Union method (will Union with itself in first For loop)
    Set b = SearchRange(1).MergeArea(1)
    
    For Each cell In SearchRange
        Set a = cell.MergeArea(1)
        Set b = Union(a, b)
    Next
    
    ' a becomes the preload for the next Union; n will be used to exclude
    ' it from the count if it's not the right color
    n = a.Interior.Color = colorRange.Interior.Color
    
    For Each cell In b
        If cell.Interior.Color = colorRange.Interior.Color And _
           cell.Font.Color = colorRange.Font.Color Then
            Set a = Union(cell, a)
        End If
    Next
    
    ColorInteriorFontCount = a.Count - 1 - n


End Function
 
Last edited:
Upvote 0
Thank you very much! It works perfectly!! :)

Try this

Code:
Function ColorInteriorFontCount(SearchRange As Range, colorRange As Range) As Long
    Dim cell As Range, a As Range, b As Range, n As Integer
    
    ' preload for Union method (will Union with itself in first For loop)
    Set b = SearchRange(1).MergeArea(1)
    
    For Each cell In SearchRange
        Set a = cell.MergeArea(1)
        Set b = Union(a, b)
    Next
    
    ' a becomes the preload for the next Union; n will be used to exclude
    ' it from the count if it's not the right color
    n = a.Interior.Color = colorRange.Interior.Color
    
    For Each cell In b
        If cell.Interior.Color = colorRange.Interior.Color And _
           cell.Font.Color = colorRange.Font.Color Then
            Set a = Union(cell, a)
        End If
    Next
    
    ColorInteriorFontCount = a.Count - 1 - n


End Function
 
Upvote 0
Try this

ABCD
1HI2
2B2
3B3
4B4
5B5
6B6
7B7
8B8

<tbody>
</tbody>

Formula
CellFormula
D1=ColorInteriorFontCount(B2:B8,C1)

<tbody>
</tbody>

<tbody>
</tbody>



Code:
Function ColorInteriorFontCount(SearchRange As Range, colorRange As Range) As Long
    Dim cell As Range, a As Range, b As Range, n As Integer
 
    ' preload for Union method (will Union with itself in first For loop)
    Set b = SearchRange(1).MergeArea(1)
 
    For Each cell In SearchRange
        Set a = cell.MergeArea(1)
        Set b = Union(a, b)
    Next
 
    ' a becomes the preload for the next Union; n will be used to exclude
    ' it from the count if it's not the right color
    n = a.Interior.Color = colorRange.Interior.Color
 
    For Each cell In b
        If cell.Interior.Color = colorRange.Interior.Color And _
           cell.Font.Color = colorRange.Font.Color Then
            Set a = Union(cell, a)
        End If
    Next
 
    ColorInteriorFontCount = a.Count - 1 - n


End Function
Hi, i try ur code but i had issue..

the count included A1 . A1 is the font colour i want to trigger.. dont know why is 3.. should be 2

Random_Macro.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1purpleredgreenorange brown123456789101112131415161718
23PPPBBBBBB
Sheet1
Cell Formulas
RangeFormula
A2A2=ColorInteriorFontCount(F2:W2,A1)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top