udf to count colour in a matrix

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
599
have a udf to count a colour
=Countcolour($A$1:$M$13,U$1)

what if i want to add a wider range , i.e.
B1:M1,C2:M2,D3:M3,E4:M4,F5:M5,G6:M6,H7:M7,I8:M8,J9:M9,K10:M10,L11:M11,M12
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,881
Hi

It would help if you post the udf. I'm sure there are several udf's with that name available on the web.
 
Last edited:

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
599
Hi

It would help if you post the udf. I'm sure there are several udf's with that name available on the web.

was copied from elsewhere

Code:
Function Countcolour(rng As Range, colour As Range) As Long
Dim c As Range
Application.Volatile
For Each c In rng
    If c.Interior.ColorIndex = colour.Interior.ColorIndex Then
        Countcolour = Countcolour + 1
    End If
Next
End Function
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,680
Office Version
  1. 2010
Platform
  1. Windows
Just pass the range you indicated encased in a set of parentheses...

=Countcolour((B1:M1,C2:M2,D3:M3,E4:M4,F5:M5,G6:M6,H7:M7,I8:M8,J9:M9,K10:M10,L11:M11,M12),A2)
 

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
599
Just pass the range you indicated encased in a set of parentheses...

=Countcolour((B1:M1,C2:M2,D3:M3,E4:M4,F5:M5,G6:M6,H7:M7,I8:M8,J9:M9,K10:M10,L11:M11,M12),A2)

thanks Rick, i did manage to do it with formula below but yours is alot better :)

=Countcolour(B$1:M$1,Y$1) + Countcolour(C$2:M$2,Y$1) + Countcolour(D$3:M$3,Y$1) + Countcolour(E$4:M$4,Y$1) + Countcolour(F$5:M$5,Y$1) + Countcolour(G$6:M$6,Y$1) + Countcolour(H$7:M$7,Y$1) + Countcolour(I$8:M$8,Y$1) + Countcolour(J$9:M$9,Y$1) + Countcolour(K$10:M$10,Y$1) + Countcolour(L$11:M$11,Y$1) + Countcolour(M$12,Y$1)
 

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
599
just noticed one sligt problem when i run a macro to clear the cells colours the formula doesnt update until i click on F9

i did add Application.Calculation = xlAutomatic but itt didnt need it before
 

Watch MrExcel Video

Forum statistics

Threads
1,122,509
Messages
5,596,558
Members
414,077
Latest member
ammylar5

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
Top