Counting colour of cells when some are merged

openedonce

New Member
Joined
Apr 28, 2014
Messages
5
Hi,

I have a VBA function in a module that counts cell colour:

Function CountCcolor(range_data As Range, criteria As Range) As Long
Dim datax As Range
Dim xcolor As Long
xcolor = criteria.Interior.ColorIndex
For Each datax In range_data
If datax.Interior.ColorIndex = xcolor Then
CountCcolor = CountCcolor + 1
End If
Next datax
End Function

I then colour fill cell A4 with the colour I wish to count and I have in A5 the following formula

=ColorCount(J$3:X$50,A4)

My function works except for merged cells, as it does not treat it as a singular entity, so 2 cells that are merged together will be counted as 2, rather than 1.

Is there a simple addition to the above function's code, which will treat merged cells as 1 colour count when it comes across them?

I realise that having merged cells on a worksheet has led to my problem but I need to use them to help the layout of the grid I have created, as a few cells have a lot more written information in than others.

Any help will be gratefully received.

Simon
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try using the cell format "Center Across Selection" instead of Merge.

Format > Alignment > Horizontal > Center Across Selection > OK

May help.

Howard
 
Upvote 0
Function CountCcolor(range_data As Range, criteria As Range) As Long
' count color cells including merged ones
Dim datax As Range
Dim xcolor As Long
Dim a, b
xcolor = criteria.Interior.ColorIndex
CountCcolor = 0
For Each datax In range_data
a = Cells(datax.Row - 1, datax.Column).Interior.ColorIndex
b = Cells(datax.Row, datax.Column - 1).Interior.ColorIndex
If datax.Interior.ColorIndex = xcolor And a <> xcolor And b <> xcolor Then
CountCcolor = CountCcolor + 1
End If
Next datax
End Function
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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