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?
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,444
Messages
5,528,797
Members
409,835
Latest member
Mafu1267

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top