counting by conditional formatting color

tbilderbac

New Member
Joined
Apr 11, 2022
Messages
28
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I got this code:

-----------
Public Function CountByColor2(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = TargetCell.Interior.ColorIndex
For Each C In CellRange
If C.Interior.ColorIndex = TargetColor Then Count = Count + 1
Next C
CountByColor2 = Count


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

I created the formula:
=CountByColor2(RC[-23]:RC[-6],R[-16]C[-25])

and wanting it to count up the RED blocks: [should be 5]

1651059535408.png
 
It is not returning the number [should be 5] it is returning #VALUE!
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Do you still have your original function, or have you deleted it?
 
Upvote 0
In that case you need to delete your original function, or rename one of them.
 
Upvote 0
oh I have renamed them one is CountByColor and the other is CountByColor2. Having both, do they interfere with each other? I can get rid of one if I need to.
 
Upvote 0
You can keep both of them as long as they do not have the same names.
 
Upvote 0
Any idea why neither of the codes are working? When I watched the video where i saw the first one it worked so easily I thought no problem??
 
Upvote 0
If you edit the cell with the formula & hit Enter, do you get any error messages?
 
Upvote 0
I edit the cell with the formula [=CountByColor(RC[-23]:RC[-6],R[-16]C[-25])] and hit Enter and #VALUE! shows in the cell.
 
Upvote 0
Is all the code I posted in a standard module?
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

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