Count by color only if another condition holds

g579

New Member
Joined
Apr 14, 2010
Messages
7
Hi,



I've been using a user defined function that I found in the forum below and I would like to manipulate it to make more use out of it and I was wondering if you know how to do this. What I am trying to do is basically count the number of cells that are green in one column only if the corresponding cell in another column contains a certain text like "Office".

Basically, I only want to count the green cells in say column A if the corresponding cell in column B contains "Office". I also would like it to refresh automatically every time a change happens.

Thank you in advance for your help!! :)

Public Function CountIfColor(rng As Range, clrindx As Integer)
Dim Cell
CountIfColor = 0
For Each Cell In rng.Cells
If Cell.Interior.ColorIndex = clrindx Then
CountIfColor = CountIfColor + 1
End If
Next Cell
End Function

To return the quantity of red-shaded (non-conditionally formatted) cells in the range A1:A100 for example, this is the formula you'd type into a cell:

=CountIfColor(A1:A100,3)
 

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.
Try this.
Code:
Public Function CountIfColor(rng As Range, clrindx As Long) As Long
Dim cl As Range
 
    Application.Volatile
    
    For Each cl In rng.Cells
        If cl.Interior.ColorIndex = clrindx And cl.Offset(, 1) = "Office" Then
            CountIfColor = CountIfColor + 1
        End If
    Next cl
    
End Function
By the way is it green or red?:)
 
Upvote 0
Thanks Norie.

That actually didn't work. Did I have to add it to the previous code or start a new one? I started a new one and it came back with a #VALUE Error.

The function I inserted in the cell was =CountIfColor(Range,color index)


p.s. I'm looking up green cells :) I changed the color index accordingly
 
Upvote 0
Oh and btw the two columns that I am trying to compare are columns B and F. So there are three columns between them.
 
Upvote 0
Well it worked fine for me.

You should replace the original code with it.

There could be all sorts of reasons for getting the #VALUE! error.

It could be something to do with the code, it could be the data, it could be the formatting.

Generally it's not a good idea to count by colour, though obviously it can be done.
 
Upvote 0
Ok I figured out the problem. It works now! you're a genius :) thank you soo much but now is there a way for it to refresh automatically once a change has been made?
 
Upvote 0
Do you have anything else you want to mention?

Green/Red, columns B/F.:eek:

The code I posted should work for 2 adjacent range, with the leftmost, eg A, being the one with the formatting and the other, eg B, with the search term.

It's probably possible to adjust the code to work with non-adjacent columns, or even to take 2 ranges as arguments.:)
 
Upvote 0
Sorry, I didn't know it wouldn't refresh automatically or I would have mentioned that from the start.

I adjusted the column offset to 4 to account for the space between the columns.

Now, really the only thing is to make it refresh automatically but I don't know if that's even possible.

Thanks
 
Upvote 0
Refresh automatically when exactly?

It seemed to refresh fine when I tried some simple tests.

eg copy/pasting, entering values etc
 
Upvote 0
I have it in my spreadsheet right now and if I make a format change to green in column B then it wouldn't update the count value automatically. It only updates it if I click in the cell where the formula is found and hit enter after the change.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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