Cell count by multiple conditions

sharpeye

Board Regular
Joined
Oct 5, 2018
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone
I need a little help. Im tring to do a count of cells based on colours. The first colour is the font which, if the font is yellow (based on the probability of a result being over a certain value, in this case 85%+.) when the result is positive, I colour the cell green, if not I colour the cell red. Id like to count all the cells in a given range that are both green in colour with yellow font. I already have the commands in place that can count cell colour and font colour but have no idea how to combine both in a count.

I count the font colour using
=CountCellsByFontColor(M2:M95,M1)

I count cell colour using
=CountCellsByColor(M2:M95,M1)

I would like to create a cell that would only count the cells that meet both criteria
Any ideas would be amazing
Many thanks
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You seem to have a couple of UDFs there. Here's an alternative that will count where both cell color and font color match a given cell.

VBA Code:
Public Function CountAllColors(RangeToCheck As Range, CellFormat As Range) As Long
    Application.Volatile
    Dim c As Range, x As Long
    x = 0
    For Each c In RangeToCheck
        If c.Font.Color = CellFormat.Font.Color And c.Interior.Color = CellFormat.Interior.Color Then x = x + 1
    Next c
    CountAllColors = x
End Function
 
Upvote 0
Hi guys, thanks for the ideas.
I havent had a chance yet to try out your suggestions but I just thought Id throw this in there as it might make for an easier solution.
The cells that I would like to count with the yellow text only has yellow text if the value of that text is 80% or above so it could be that instead of counting its text colour, its value could be counted instead.

Just to confirm, there are 4 different colour combinations in the cell range to be counted,
Green cell below 80% or Black text
Red cell below 80%, Black text
Green cell above 80% or Yellow text (These are the cells Id like to count)
Red cell above 80%, Yellow text

Hope this helps and apologies for not including this in my original post
 
Upvote 0
What column has the percentages that you are referring to ?
What is the formula / criteria for the interior color ?
What is the formula / criteria for the font color ?
Without referring to color what is it you are wanting to count ?
 
Upvote 0
This is a sample of the values Im using. The Yellow font is generated using a conditional format where the value >= 80%
The Red and Green cell colours are manually filled in by me depending on a true or false result.
What I need is a formula that will count the green cells that contain yellow text or green cells with a value of >=80%
In this sample there are 13 cells id like to count out of the 25 cells in the sample

1659289905051.png
 
Upvote 0
I think you may need to explain the conditional formatting rules you are using. Example:

Book2
ABCD
181%
291%
311%
486%
596%
611%
781%
891%
911%
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:A9Cell Value>0.95textNO
A1:A9Cell Value>0.85textNO
A1:A9Cell Value>0.9textNO
A1:A9Cell Value>0.8textNO


Using this free tool can make that easier
 
Upvote 0
I think you may need to explain the conditional formatting rules you are using. Example:

Book2
ABCD
181%
291%
311%
486%
596%
611%
781%
891%
911%
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:A9Cell Value>0.95textNO
A1:A9Cell Value>0.85textNO
A1:A9Cell Value>0.9textNO
A1:A9Cell Value>0.8textNO


Using this free tool can make that easier
I basically use a conditional formatting rule to colour any value 80% or over in yellow text and then I remove the conditional formatting rules and the text stays yellow.
I then change the cell colour to either red or green manually so by the time I get to the stage where i need to count the green cells with yellow text, there is no conditional formtting in play
 
Upvote 0
Green cell above 80% or Yellow text (These are the cells Id like to count)

Book2
ABC
185%1
291%0
311%0
486%0
596%1
611%0
755%0
891%0
982%1
1091%0
113<- Number of cells where color is green and value >= 80%
Sheet1
Cell Formulas
RangeFormula
B1:B10B1=IF(AND(CellColor=4,A1>=0.8),1,0)
B11B11=SUM(B1:B10)


Where the formula CellColor is defined as a formula using the name manager and the Get.Cell function

Name: "CellColor". RefersTo "=GET.CELL(63,INDIRECT("rc[-1]",FALSE))"
 
Upvote 0
The way I interpreted your original post was that you were using UDFs to count the number of cells in a given range - M2:M95 - where the cells' format matched the format of another given cell - M1. The UDF I wrote in post #2, when placed in a standard module and applied to the same ranges using some sample data, works like this:

test.xlsb
MNOPQRS
11%= 'template' cell
281%46= Count of cells in M2:M95 matching 'template'
391%
473%
585%
675%
776%
895%
987%
1074%
1185%
1284%
1373%
1481%
1577%
1672%
1793%
1887%
1994%
2093%
2189%
2298%
2375%
2488%
2592%
2670%
2781%
2881%
2991%
3073%
3185%
3275%
3376%
3495%
3587%
3674%
3785%
3884%
3973%
4081%
4177%
4272%
4393%
4487%
4594%
4693%
4789%
4898%
4975%
5088%
5192%
5270%
5381%
5481%
5591%
5673%
5785%
5875%
5976%
6095%
6187%
6274%
6385%
6484%
6573%
6681%
6777%
6872%
6993%
7087%
7194%
7293%
7389%
7498%
7575%
7688%
7792%
7870%
7981%
8081%
8191%
8273%
8385%
8475%
8576%
8695%
8787%
8874%
8985%
9084%
9173%
9281%
9377%
9472%
9593%
96
Sheet3
Cell Formulas
RangeFormula
N2N2=CountAllColors(M2:M95,M1)


I'd be keen to know if you were able to try it out, or if I'd misinterpreted your original intention.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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