Using ColorFunction with an added variable

neil.crawford

New Member
Joined
Feb 15, 2006
Messages
17
To count cells of a given color, I'm presently using this formula, =ColorFunction($L$2,Desktops!$D$3:$D$77).

I need to segregate the counts based on another column as well. For instance, "Count all the cells on the Desktop worksheet/Column D with the color found in L2, if the value in Column B of the same row is 4X."

How to do it?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

venkat1926

Well-known Member
Joined
Aug 21, 2005
Messages
4,824
i did not use countif . I have given a macro see whether it helps.
see comment in the code.

the macro is
Code:
Sub colorfind()
Dim clr
Dim c  As Range
Dim y As Long
 clr = Range("L2").Font.ColorIndex
y = 0
Set rng = Range(Range("d1"), Range("d1").End(xlDown))
'modify the above line to suit you
For Each c In rng
If c.Font.ColorIndex = clr Then
If c.Offset(0, -2) = "4x" Then
y = y + 1
End If
End If
Next
MsgBox y
End Sub
 
L

Legacy 98055

Guest
I am not sure which ColorFunction you are using as there are a handful of them out there. I just stole the one from Ozgrid and edited it...

neil.crawford.zip

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Function</font> ColorFunction(rColor <font color="#0000A0">As</font> Range, rRange <font color="#0000A0">As</font> Range, OffsetColumns, OffsetValue)
       <font color="#0000A0">Dim</font> rCell <font color="#0000A0">As</font> Range
       <font color="#0000A0">Dim</font> lCol <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">Dim</font> vResult

  <font color="#008000">''''''''''''''''''''''''''''''''''''''</font>
  <font color="#008000">'Written by Ozgrid Business Applications</font>
  <font color="#008000">'www.ozgrid.com</font>

  <font color="#008000">'Sums or counts cells based on a specified fill color.</font>
  <font color="#008000">'''''''''''''''''''''''''''''''''''''''</font>

       lCol = rColor.Interior.ColorIndex


       <font color="#0000A0">For</font> <font color="#0000A0">Each</font> rCell <font color="#0000A0">In</font> rRange
           <font color="#0000A0">If</font> rCell.Interior.ColorIndex = lCol <font color="#0000A0">And</font> rCell.Offset(, OffsetColumns) = OffsetValue <font color="#0000A0">Then</font>
               vResult = WorksheetFunction.SUM(rCell, vResult)
           <font color="#0000A0">End</font> <font color="#0000A0">If</font>
       <font color="#0000A0">Next</font> rCell


      ColorFunction = vResult
  <font color="#0000A0">End</font> <font color="#0000A0">Function</font>
</FONT></td></tr></table><button onclick='document.all("9142006231243281").value=document.all("9142006231243281").value.replace(/<br \/>\s\s/g,"");document.all("9142006231243281").value=document.all("9142006231243281").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("9142006231243281").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="9142006231243281" wrap="virtual">
Function ColorFunction(rColor As Range, rRange As Range, OffsetColumns, OffsetValue)
Dim rCell As Range
Dim lCol As Long
Dim vResult

''''''''''''''''''''''''''''''''''''''
'Written by Ozgrid Business Applications
'www.ozgrid.com

'Sums or counts cells based on a specified fill color.
'''''''''''''''''''''''''''''''''''''''

lCol = rColor.Interior.ColorIndex


For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol And rCell.Offset(, OffsetColumns) = OffsetValue Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell


ColorFunction = vResult
End Function</textarea>

neil.crawford.zip
 

neil.crawford

New Member
Joined
Feb 15, 2006
Messages
17
OK. That almost works.

=colorfunction(L2,D:D,-2,"4X")

What does the -2 do in your formula?

Is there a purpose in hiding Column F?

It appears your formula sums the contents of D:D. I need it to count the cells of D:D.
 

Forum statistics

Threads
1,141,136
Messages
5,704,485
Members
421,353
Latest member
jekoxien15

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