Countbycolor in conditional formatted cells VBA?

praktikant

New Member
Joined
Dec 9, 2014
Messages
9
Hi guys,

I've been working on a spreadsheet for some time now and looked through several forums and threads to find an answer, but cant find any at all.

The sheet is more or less like a gantt-chart with activities down column A and duration/weeks along the rows

http://tinypic.com/r/xnfb6p/8

I've made it so the user only have to enter the start week and the duration (how many weeks) in the cells and conditional formatted will do the rest by coloring the duration orange.

As of now there are 154 activities with different start dates and durations, which is why I want a color count of activities on a weekly basis.


I tried:

Function CountByColor(CellColor As Range, CountRange As
Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As
Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In
CountRange
If ICol = TCell.Interior.ColorIndex Then
CountByColor =
CountByColor + 1
End If
Next TCell
End Function

and

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function

When I use the countbycolor it will count colored cells, but NOT the conditional formatted which is the purpose of the entire thing.

http://tinypic.com/r/2rddxsy/8

Can any of you see what i'm doing wrong?
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
xnfb6p.jpg
 
Upvote 0
praktikant,

Welcome top MrExcel.

The problem is indeed the fact that you are trying to count based on conditional cell colour. There is no direct way to do this.
You could check out this Rick Rothstein article on the subject and see if it helps....
Get Displayed Cell Color (whether from Conditional Formatting or not)

I've not thought this through 100% but as a workaround can you have formula in the activity cells that will give 'live' activity cells a value?
Any value, say 'x'
Then amend the conditional formatting so that the font is same colour as background so as to make the 'x' 'invisible'.
Then do your counts based on counting cells with value 'x'

Hope that helps.
 
Upvote 0


Hi VoG, I've also tried that one :biggrin:

praktikant,

Welcome top MrExcel.

The problem is indeed the fact that you are trying to count based on conditional cell colour. There is no direct way to do this.
You could check out this Rick Rothstein article on the subject and see if it helps....
Get Displayed Cell Color (whether from Conditional Formatting or not)

I've not thought this through 100% but as a workaround can you have formula in the activity cells that will give 'live' activity cells a value?
Any value, say 'x'
Then amend the conditional formatting so that the font is same colour as background so as to make the 'x' 'invisible'.
Then do your counts based on counting cells with value 'x'

Hope that helps.

Thanke you Snakehips, I'll have a look at it. Not entirely sure, how to try it out, but i'll for sure give it a go.
 
Upvote 0
Something like this...
Excel Workbook
CDEFGHIJKLMNOPQ
7ActivityStartDurationWeek
81234567891011
9
10Acty 125 xxxxx
11Acty 253xxx
12Acty 317xxxxxxx
13Acty 439xxxxxxxxx
14
15
16WeekTotal12334431111
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G101. / Cell Value equal to ="x"Abc


If you set the cf so that the font is same as background then you will not see the x's.




 
Upvote 0
That worked out perfectly! No complaints from the users and way more simple compared to what I was trying to do.
Would have been nice to count the colored conditional formatted cells, but for the purpose of my project, your solution is just perfect! Thanks alot!

Something like this...
Sheet1

CDEFGHIJKLMNOPQ
ActivityStartDuration
Acty 1
Acty 2
Acty 3
Acty 4
Total

<colgroup><col style="width: 30px; font-weight: bold;"><col style="width: 127px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 42px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 23px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 25px;"><col style="width: 25px;"></colgroup><tbody>
[TD="bgcolor: #CACACA, align: center"]7[/TD]

[TD="align: center"]Week[/TD]

[TD="bgcolor: #CACACA, align: center"]8[/TD]

[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]7[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]10[/TD]
[TD="align: center"]11[/TD]

[TD="bgcolor: #CACACA, align: center"]9[/TD]

[TD="bgcolor: #CACACA, align: center"]10[/TD]

[TD="align: center"]2[/TD]
[TD="align: center"]5[/TD]

[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]

[TD="bgcolor: #CACACA, align: center"]11[/TD]

[TD="align: center"]5[/TD]
[TD="align: center"]3[/TD]

[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]

[TD="bgcolor: #CACACA, align: center"]12[/TD]

[TD="align: center"]1[/TD]
[TD="align: center"]7[/TD]

[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]

[TD="bgcolor: #CACACA, align: center"]13[/TD]

[TD="align: center"]3[/TD]
[TD="align: center"]9[/TD]

[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]
[TD="bgcolor: #FF0000, align: center"]x[/TD]

[TD="bgcolor: #CACACA, align: center"]14[/TD]

[TD="bgcolor: #CACACA, align: center"]15[/TD]

[TD="bgcolor: #CACACA, align: center"]16[/TD]

[TD="align: center"]Week[/TD]

[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]

</tbody>

Spreadsheet Formulas
CellFormula
G10=IF(AND(G$8>=$D10,G$8<=$D10+$E10-1),"x","")
G16=COUNTIF(G10:G15,"=x")

<tbody>
</tbody>

<tbody>
</tbody>

Conditional formatting
CellNr.: / ConditionFormat
G101. / Cell Value equal to ="x"

<tbody>
[TD="bgcolor: #FF0000"]Abc

<tbody>

</tbody>
[/TD]

</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

If you set the cf so that the font is same as background then you will not see the x's.




 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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