# CALCULATING COLOUR

#### mrbeanyuk

##### Board Regular
Right, an interesting one for you guys.....

I have a spreadsheet with many different colours relating to various subjects. I am looking for a formula that will count the number of one colour in a particular column. Is this achieveable?

NB: The colours are manually input and not conditionally formatted.

Thanks!

### Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Good morning mrbeanyuk

Two choices : if you're farily well up on mcaros, have a look here for some custom written functions to add to your spreadsheet :

http://www.cpearson.com/excel/colors.htm

=FontColour(A1)
=CellColour(A1)
=SumFontCol(A1:A100,5)
=SumCellCol(A1:A100,10)
=CountFontCol(A1:A100,5)
=CountCellCol(A1:A100,10)

HTH

DominicB

Hi MrBeanyUK

The following is a fairly trivial UDF that uses three arguments (a cell coloured with the colour you want to count, the range from which to count relevantly coloured cells, and an optional third argument which, if left blank, will count the cells, or if any other value than 1 is entered, will sum the relevant cells):

Code:
``````Function ColourCells(RefCell As Range, TargetCells As Range, Optional SumOrCount As Integer = 1) As Double
Dim iColIndex As Integer, c As Range, temp As Double
iColIndex = RefCell.Interior.ColorIndex
temp = 0
For Each c In TargetCells
If SumOrCount = 1 Then
If c.Interior.ColorIndex = iColIndex Then temp = temp + 1
Else
If c.Interior.ColorIndex = iColIndex Then temp = temp + c.Value
End If
Next c
ColourCells = temp
End Function``````

So, if you had a range in A1:A20 and you wanted to count the red cells, you could use the formula thus:

Code:
``=ColourCells(B1,A1:A20)``

which assumes you have coloured B1 red also (from the toolbar).

Hope this helps!

Richard

With the VB coding, would you mind writing it for me please!?!?!?!

I want C,2 of 'sheet2' to count the number of yellow cells in column C of the 'sheet1'

Sorry and thanks!

Copy the code I posted from "Function Colour Cells.... End Function" and in your spreadsheet go Tools>Macro>Visual Basic Editor which will open up a new window. In the top left of the new window you should be able to see your workbook's name listed there. Right-click on it and go Insert>Module and paste the function code in there. Once you've done that, you can close the VBE down. Back in your spreadsheet, on Sheet2, select a cell and colour it yellow using the toolbar button (ie so this will be the same colour you want to count) - say this is C1. Then, in C2 enter:

Code:
``=ColourCells(C1,Sheet1!C:C)``

Richard

Thanks for that! It worked a treat! The VB Coding was brilliant!

Replies
6
Views
315
Replies
7
Views
367
Replies
10
Views
737
Replies
3
Views
273
L
Replies
1
Views
259

1,219,100
Messages
6,146,281
Members
450,684
Latest member
wilto1tg

### 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.

### Which adblocker are you using?

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

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