Counting Cells by Color

Slicemahn

Board Regular
Joined
Jun 10, 2004
Messages
120
Hello MrExcel Nation!

I am setting up a planning worksheet to see where I am over/under utilized. The challenge I am having is counting cells by color. There are some activities that require 2 resources and the other cells that have text without any cell color (or white) require just one resource. Now easily I can do a COUNTA function to capture the cells with text but how can I add 2 resources for every gray cell without manually typing in a calculation?

Book1
ABCDEF
1FacilityNorthSouthEastWestCentral
2Activity1TecTecTec
3Activity2TecTecTec
4Activity3
5Activity4
6Activity5
7Activity6
8Activity7Tec
9Activity8Tec
10Activity9Tec
11Activity10Tec
12Activity11Tec
13Activity12
14Activity13
15Activity14
16Activity15
17Activity16
18Activity17
19Activity18
20Total Required94763
Sheet1


Thank you for your help! This is difficult for me.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
There are a ton of threads on this.
I just wrote this up place a colored cell in J1 and the code will loop through the range and add up the cells that match the color.

VBA Code:
Sub Button1_Click()
    Dim sh          As Worksheet, rng As Range, c As Range
    Dim clr         As String
    Set sh = ActiveSheet
    With sh
        clr = .Range("J1").Interior.Color
        Set rng = .Range("B1:F" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each c In rng.Cells
            If c.Interior.Color = clr Then
                .Range("I1") = .Range("I1") + 1
            End If
        Next
    End With
End Sub

-
1683828540540.png
 
Upvote 0
Hi Davesexcel,
Thank for this solution! But I am interested in counting by location so the columns North, South, East, West and Central. So for North there are three "Tecs" and three gray boxes. Well the three gray boxes should count as two. Therefore the total required will be COUNTA(B2:B19) + Function(3 gray cells * 2) to give a total of 9. That is what I am looking to do. So can your code translate to a function that can add these two factors (text cells + gray colored cells)?
 
Upvote 0
A possible solution, put a number 2 in each of the gray cells and set the font color to the same color as the fill color... you can then use a simple formula to sum the column and add it to a COUNTA calculation for the column to get your totals. You could create a macro to do the filling with gray and entering the 2 and assign that to shortcut (or a button) to make filling the gray cells easier.
 
Upvote 0
A possible solution, put a number 2 in each of the gray cells and set the font color to the same color as the fill color... you can then use a simple formula to sum the column and add it to a COUNTA calculation for the column to get your totals. You could create a macro to do the filling with gray and entering the 2 and assign that to shortcut (or a button) to make filling the gray cells easier.
A good solution but I don’t have the time to go through 40 weeks of planning by 5 different locations. I was hopeful of a function that would compile the results quickly.
 
Upvote 0
Just so I understand your setup, does 40 weeks mean 40 sheets?

If so, are there any other sheets in the workbook besides those 40 sheets?

Also, if so, then are those 40 sheets all setup the same (5 columns each with the same number of activities?
 
Upvote 0
Unfortunately no. It is a workbook for every region with tabs for each week in a month. So each month tab you will see the locations for different number of activities, because each plant site operates on different projects.
 
Upvote 0
The sample you provided does not match your explanation.
Column D should be 11, E should be 9, F should be 4

Here is a Function:
VBA Code:
Function aa_sumPlusClr(rng As Range, colr As Range)
    Dim sh As Worksheet
    Dim c As Range
    Dim x As Long, t
    Set sh = ActiveSheet
    With sh
        For Each c In rng.Cells
            If c.Interior.Color = colr.Interior.Color Then
                x = x + 1
            End If
        Next
    End With
    t = Application.WorksheetFunction.CountIf(rng, "*")
    aa_sumPlusClr = t + x * 2
    
End Function
In B20 enter this formula and drag accross
Excel Formula:
=aa_sumPlusClr(B2:B19,$J$1)

J1 is the cell that has the color to find
 

Attachments

  • 1683898887082.png
    1683898887082.png
    22.9 KB · Views: 7
Upvote 1
Solution
The sample you provided does not match your explanation.
Column D should be 11, E should be 9, F should be 4

Here is a Function:
VBA Code:
Function aa_sumPlusClr(rng As Range, colr As Range)
    Dim sh As Worksheet
    Dim c As Range
    Dim x As Long, t
    Set sh = ActiveSheet
    With sh
        For Each c In rng.Cells
            If c.Interior.Color = colr.Interior.Color Then
                x = x + 1
            End If
        Next
    End With
    t = Application.WorksheetFunction.CountIf(rng, "*")
    aa_sumPlusClr = t + x * 2
   
End Function
In B20 enter this formula and drag accross
Excel Formula:
=aa_sumPlusClr(B2:B19,$J$1)

J1 is the cell that has the color to find
I love this! Thanks Dave! Works great. Thanks a million.
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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