What the Tech logo
Welcome! Register for a free account (or login) > How does it work?
  1. Quickly register. It will only take 60 seconds.
  2. Start a new topic. Ask your question. Wait for an email reply.
  3. Is your system infected? Begin reading the malware removal guide.
register button
Reply to this topicStart new topic
> • Counting Colours in Excel •, - Microsoft Excel 2007 -
manicd
post Sep 13 2009, 10:34 AM
Post #1


Silver Member
***

Group: Authentic Member
Posts: 410
Joined: 15-June 07
From: England, UK
Member No.: 70,781
Operating System: • Windows Vista - Home Basic sp2•



Hi

I was wondering if anyone could help me with counting the number of cells filled with a colour in Excel.
I've googled the question which has provided many answers, but I just don't understand them. This is an example of one of these sites HERE.
What I'm trying to do is count the number of cells which are filled Yellow & Grey in each column lettered C - AY;
Attached Image

What I do understand is that you need to use the VBA and insert a Module containing a formula that will perform this for me. I also need the formula to automatically update when I enter a colour in to a cell.
I know this is a big ask but if anyone can at least guide me on doing this it would greatly appreciated.


This post has been edited by manicd: Sep 13 2009, 11:08 AM
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies (1 - 10)
manicd
post Sep 15 2009, 12:03 PM
Post #2


Silver Member
***

Group: Authentic Member
Posts: 410
Joined: 15-June 07
From: England, UK
Member No.: 70,781
Operating System: • Windows Vista - Home Basic sp2•



Anyone please. There must be some Macro wizards out there who can help me.

This post has been edited by manicd: Sep 15 2009, 12:04 PM
Go to the top of the page
 
+Quote Post
Glaswegian
post Sep 15 2009, 03:07 PM
Post #3


New Member
*

Group: Visiting Staff
Posts: 6
Joined: 14-September 09
From: Glasgow
Member No.: 87,920
Operating System: Win XP SP3 / Win 7 RC



Hi there

Chip Pearson's method is the best - and the one I would have pointed you towards had you not already found it.

However, if it's just a simple count by colour you need, then you could try this previous version of Chip's function. It doesn't include any other functions - just the simple count. And you don't need to download the module. I've used this in the past and it works just fine.

From Excel, press Alt+F11 to open the VBE. Add a new module and copy and paste the following into the module
CODE
Function CountByColor(InRange As Range, _
    WhatColorIndex As Integer, _
    Optional OfText As Boolean = False) As Long
'
' This function return the number of cells in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
'
Dim rng As Range
Application.Volatile True

For Each rng In InRange.Cells
If OfText = True Then
    CountByColor = CountByColor - _
            (rng.Font.ColorIndex = WhatColorIndex)
Else
    CountByColor = CountByColor - _
       (rng.Interior.ColorIndex = WhatColorIndex)
End If
Next rng

End Function

This is the function that does the counting.

Of course, you need to know the index number of each colour. To find the number, copy this code and run it on another sheet
CODE
Sub ListColours()
Dim x As Integer

For x = 1 To 56
    ActiveSheet.Cells(x, 1).Interior.ColorIndex = x
Next
End Sub

This will list all the colours available - just read off the colour number against the row number. For example, yellow = 6.

Now you can insert the formula in your total row
=CountByColor(C1:C5,6,FALSE) or use Insert Function to choose from the User Defined List.

The function should re-calculate automatically as it uses Application.Volatile - you'll probably need to click in another cell to ensure it actually does it.

Any problems just post back.


Go to the top of the page
 
+Quote Post
manicd
post Sep 16 2009, 02:17 PM
Post #4


Silver Member
***

Group: Authentic Member
Posts: 410
Joined: 15-June 07
From: England, UK
Member No.: 70,781
Operating System: • Windows Vista - Home Basic sp2•



Hi Glaswegian & thanks that seems to have done the trick, cheers.
Go to the top of the page
 
+Quote Post
manicd
post Sep 19 2009, 04:31 AM
Post #5


Silver Member
***

Group: Authentic Member
Posts: 410
Joined: 15-June 07
From: England, UK
Member No.: 70,781
Operating System: • Windows Vista - Home Basic sp2•



Hi Glaswegian
Attached Image

Just one more question.
How do I count two separate colours in the same column with the =CountByColor(C1:C5,6,FALSE) formula?
Also is there anything I can put in to the formula so I don't have to keep clicking on the total's row when I add a colour?


This post has been edited by manicd: Sep 19 2009, 11:39 AM
Go to the top of the page
 
+Quote Post
Glaswegian
post Sep 19 2009, 01:40 PM
Post #6


New Member
*

Group: Visiting Staff
Posts: 6
Joined: 14-September 09
From: Glasgow
Member No.: 87,920
Operating System: Win XP SP3 / Win 7 RC



Hi

I'm not aware of any way of doing that. Perhaps you would need to put the functions in a VBA routine and then output to the sheet. Because counting colours is not a native Excel function, the one I posted earlier, and the others on Chip Pearson's site are a way around Excel's limitations.

The same applies for your second question - even if you were using code to do this, you would need the colorindex value - which you would have to include in your code. So any changes would require code changes. I appreciate it's not very efficient.

An alternative would be to look again at the way your data is presented. Is a coloured cell the only way? The beauty of Excel is that there are often many different ways to reach the same result.
Go to the top of the page
 
+Quote Post
manicd
post Sep 20 2009, 01:36 PM
Post #7


Silver Member
***

Group: Authentic Member
Posts: 410
Joined: 15-June 07
From: England, UK
Member No.: 70,781
Operating System: • Windows Vista - Home Basic sp2•



Thanks Glaswegian for all your help & suggestions
Go to the top of the page
 
+Quote Post
Juste
post Oct 4 2009, 01:03 PM
Post #8


New Member
*

Group: New Member
Posts: 2
Joined: 4-October 09
Member No.: 88,231
Operating System: vista



i dont know, but i cant make it work... sad.gif it always shows error in formula.... tired of trying... why is that?... can someone help me , please?......
Go to the top of the page
 
+Quote Post
Glaswegian
post Oct 4 2009, 01:41 PM
Post #9


New Member
*

Group: Visiting Staff
Posts: 6
Joined: 14-September 09
From: Glasgow
Member No.: 87,920
Operating System: Win XP SP3 / Win 7 RC



Hi and welcome.

Can you give me some more information?

Where is the error? What error message do you receive? The more information you provide, the better chance we have of working out a solution.
Go to the top of the page
 
+Quote Post
Juste
post Oct 4 2009, 03:13 PM
Post #10


New Member
*

Group: New Member
Posts: 2
Joined: 4-October 09
Member No.: 88,231
Operating System: vista



it says:
'the formula you typed contains an error.'

but i think i did everything correctly: Alt + F11, then the code, saving with Alt + Q, then typing formula and it doesnt recognize.... sad.gif i have tried different codes (i found on the internet), differen formulas, but nothing.... it always shows error... thanks.
Go to the top of the page
 
+Quote Post
Glaswegian
post Oct 5 2009, 04:29 PM
Post #11


New Member
*

Group: Visiting Staff
Posts: 6
Joined: 14-September 09
From: Glasgow
Member No.: 87,920
Operating System: Win XP SP3 / Win 7 RC



Hi

That often indicates a missing bracket, or a bad reference or a missing comma etc. Can you post the formula you have that is showing the error?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   2 Denuna 421 11th December 2009 - 09:56 PM
Last post by: Doug
No New Posts   1 Cymbol 541 24th November 2009 - 10:07 PM
Last post by: appleoddity
No New Posts   0 packerman1975 1,199 13th November 2009 - 11:30 AM
Last post by: packerman1975
No New Posts   0 sunter 440 1st November 2009 - 11:11 AM
Last post by: sunter

RSS Time is now: 17th March 2010 - 01:05 AM
Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk.
Member site: Alliance of Security Analysis Professionals | UNITE Against Malware
Memory Forums | Auto Repair Forum
© Geeks to Go, Inc. | All Rights Reserved | Privacy Policy