How to combine countcellsbycolor with sumproduct?

bksparkz

New Member
Joined
Mar 20, 2014
Messages
36
Good day All,
I am having trouble combining two formulas.

I have the following formula ....

=SUMPRODUCT(--(I2:I5000="YES"),--(J2:J5000=L2))

and I need to combine it with .....

=CountCellsByColor(F2:F5000,G17)

Basically I need to count all cells that are I2:I5000="YES" and J2:J5000=L2 and F2:F5000 with a color of G17

Thank You
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

adam087

Well-known Member
Joined
Jun 7, 2010
Messages
1,356
Apologies, I'm not familiar with CountCellsByColor - what's the code within that?
I'm guessing though that you'll need a function that returns the cell colour and then use this as part of a comparison in the SUMPRODUCT. But if you post the CCBC code I'm sure we can adapt...

/AJ
 

bksparkz

New Member
Joined
Mar 20, 2014
Messages
36
The following is the function in which I used for the CCBC

Function</SPAN> GetCellColor(xlRange As</SPAN> Range) </SPAN>
Dim</SPAN> indRow, indColumn As</SPAN> Long</SPAN>
Dim</SPAN> arResults() </SPAN>

Application.Volatile </SPAN>

If</SPAN> xlRange Is</SPAN> Nothing</SPAN> Then</SPAN>
Set</SPAN> xlRange = Application.ThisCell </SPAN>
End</SPAN> If</SPAN>

If</SPAN> xlRange.Count > 1 Then</SPAN>
ReDim</SPAN> arResults(1 To</SPAN> xlRange.Rows.Count, 1 To</SPAN> xlRange.Columns.Count) </SPAN>
For</SPAN> indRow = 1 To</SPAN> xlRange.Rows.Count </SPAN>
For</SPAN> indColumn = 1 To</SPAN> xlRange.Columns.Count </SPAN>
arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color </SPAN>
Next</SPAN>
Next</SPAN>
GetCellColor = arResults </SPAN>
Else</SPAN>
GetCellColor = xlRange.Interior.Color </SPAN>
End</SPAN> If</SPAN>
End</SPAN> Function</SPAN>

Function</SPAN> GetCellFontColor(xlRange As</SPAN> Range) </SPAN>
Dim</SPAN> indRow, indColumn As</SPAN> Long</SPAN>
Dim</SPAN> arResults() </SPAN>

Application.Volatile </SPAN>

If</SPAN> xlRange Is</SPAN> Nothing</SPAN> Then</SPAN>
Set</SPAN> xlRange = Application.ThisCell </SPAN>
End</SPAN> If</SPAN>

If</SPAN> xlRange.Count > 1 Then</SPAN>
ReDim</SPAN> arResults(1 To</SPAN> xlRange.Rows.Count, 1 To</SPAN> xlRange.Columns.Count) </SPAN>
For</SPAN> indRow = 1 To</SPAN> xlRange.Rows.Count </SPAN>
For</SPAN> indColumn = 1 To</SPAN> xlRange.Columns.Count </SPAN>
arResults(indRow, indColumn) = xlRange(indRow, indColumn).Font.Color </SPAN>
Next</SPAN>
Next</SPAN>
GetCellFontColor = arResults </SPAN>
Else</SPAN>
GetCellFontColor = xlRange.Font.Color </SPAN>
End</SPAN> If</SPAN>

End</SPAN> Function</SPAN>

Function</SPAN> CountCellsByColor(rData As</SPAN> Range, cellRefColor As</SPAN> Range) As</SPAN> Long</SPAN>
Dim</SPAN> indRefColor As</SPAN> Long</SPAN>
Dim</SPAN> cellCurrent As</SPAN> Range </SPAN>
Dim</SPAN> cntRes As</SPAN> Long</SPAN>

Application.Volatile </SPAN>
cntRes = 0 </SPAN>
indRefColor = cellRefColor.Cells(1, 1).Interior.Color </SPAN>
For</SPAN> Each</SPAN> cellCurrent In</SPAN> rData </SPAN>
If</SPAN> indRefColor = cellCurrent.Interior.Color Then</SPAN>
cntRes = cntRes + 1 </SPAN>
End</SPAN> If</SPAN>
Next</SPAN> cellCurrent </SPAN>

CountCellsByColor = cntRes </SPAN>
End</SPAN> Function</SPAN>

Function</SPAN> SumCellsByColor(rData As</SPAN> Range, cellRefColor As</SPAN> Range) </SPAN>
Dim</SPAN> indRefColor As</SPAN> Long</SPAN>
Dim</SPAN> cellCurrent As</SPAN> Range </SPAN>
Dim</SPAN> sumRes </SPAN>

Application.Volatile </SPAN>
sumRes = 0 </SPAN>
indRefColor = cellRefColor.Cells(1, 1).Interior.Color </SPAN>
For</SPAN> Each</SPAN> cellCurrent In</SPAN> rData </SPAN>
If</SPAN> indRefColor = cellCurrent.Interior.Color Then</SPAN>
sumRes = WorksheetFunction.Sum(cellCurrent, sumRes) </SPAN>
End</SPAN> If</SPAN>
Next</SPAN> cellCurrent </SPAN>

SumCellsByColor = sumRes </SPAN>
End</SPAN> Function</SPAN>

Function</SPAN> CountCellsByFontColor(rData As</SPAN> Range, cellRefColor As</SPAN> Range) As</SPAN> Long</SPAN>
Dim</SPAN> indRefColor As</SPAN> Long</SPAN>
Dim</SPAN> cellCurrent As</SPAN> Range </SPAN>
Dim</SPAN> cntRes As</SPAN> Long</SPAN>

Application.Volatile </SPAN>
cntRes = 0 </SPAN>
indRefColor = cellRefColor.Cells(1, 1).Font.Color </SPAN>
For</SPAN> Each</SPAN> cellCurrent In</SPAN> rData </SPAN>
If</SPAN> indRefColor = cellCurrent.Font.Color Then</SPAN>
cntRes = cntRes + 1 </SPAN>
End</SPAN> If</SPAN>
Next</SPAN> cellCurrent </SPAN>

CountCellsByFontColor = cntRes </SPAN>
End</SPAN> Function</SPAN>

Function</SPAN> SumCellsByFontColor(rData As</SPAN> Range, cellRefColor As</SPAN> Range) </SPAN>
Dim</SPAN> indRefColor As</SPAN> Long</SPAN>
Dim</SPAN> cellCurrent As</SPAN> Range </SPAN>
Dim</SPAN> sumRes </SPAN>

Application.Volatile </SPAN>
sumRes = 0 </SPAN>
indRefColor = cellRefColor.Cells(1, 1).Font.Color </SPAN>
For</SPAN> Each</SPAN> cellCurrent In</SPAN> rData </SPAN>
If</SPAN> indRefColor = cellCurrent.Font.Color Then</SPAN>
sumRes = WorksheetFunction.Sum(cellCurrent, sumRes) </SPAN>
End</SPAN> If</SPAN>
Next</SPAN> cellCurrent </SPAN>

SumCellsByFontColor = sumRes </SPAN>
End</SPAN> Function</SPAN>
 

adam087

Well-known Member
Joined
Jun 7, 2010
Messages
1,356
OK cool. We can use the GetCellColor formula. I've mocked up the below and you can change the references as you need...


<b>Excel 2010</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #BBB"><colgroup><col width="25px" style="background-color: #DAE7F5" /><col /><col /><col /></colgroup><thead><tr style=" background-color: #DAE7F5;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="font-weight: bold;text-align: center;;">Yes or No</td><td style="font-weight: bold;text-align: center;;">Match L2</td><td style="font-weight: bold;text-align: center;;">Coloured</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">1</td><td style="text-align: center;background-color: #16365C;;"></td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">2</td><td style="text-align: center;background-color: #E4DFEC;;"></td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">3</td><td style="text-align: center;background-color: #DA9694;;"></td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">1</td><td style="text-align: center;background-color: #16365C;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">2</td><td style="text-align: center;background-color: #16365C;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">1</td><td style="text-align: center;background-color: #16365C;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: center;;">Yes</td><td style="text-align: center;;">1</td><td style="text-align: center;background-color: #DA9694;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="text-align: center;;">No</td><td style="text-align: center;;">2</td><td style="text-align: center;background-color: #16365C;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="text-align: center;;">No</td><td style="text-align: center;;">1</td><td style="text-align: center;background-color: #E4DFEC;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style="text-align: center;;">No</td><td style="text-align: center;;">1</td><td style="text-align: center;background-color: #16365C;;"></td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style="text-align: center;;"></td><td style="text-align: center;;"></td><td style="text-align: center;;"></td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style="text-align: center;color: #FFFFFF;background-color: #16365C;;">1</td><td style="font-weight: bold;text-align: center;;">3</td><td style="font-weight: bold;text-align: center;;"><-- Result</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #BBB;border-top:none;text-align: center;background-color: #DAE7F5;color: #161120">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Array Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #BBB"><thead><tr style=" background-color: #DAE7F5;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #DAE7F5;color: #161120">B13</th><td style="text-align:left">{=SUM(<font color="Blue">(<font color="Red">$A$2:$A$11="Yes"</font>)*(<font color="Red">$B$2:$B$11=A13</font>)*(<font color="Red">GetCellColor(<font color="Green">$C$2:$C$11</font>)=GetCellColor(<font color="Green">A13</font>)</font>)</font>)}</td></tr></tbody></table><b>Entered with Ctrl+Shift+Enter.</b> If entered correctly, Excel will surround with curly braces {}.
<b>Note: Do not try and enter the {} manually yourself</b></td></tr></table><br />


Hope that helps.


/AJ
 

bksparkz

New Member
Joined
Mar 20, 2014
Messages
36
Thank you for all your help. The formula you gave me made it work.

Thank You
 

Watch MrExcel Video

Forum statistics

Threads
1,109,433
Messages
5,528,738
Members
409,831
Latest member
KT50

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