Welcome! Register for a free account (or login) > How does it work?
|
|


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• |
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; 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 |
|
|
|
||
![]() |
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 |
|
|
|
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. |
|
|
|
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.
|
|
|
|
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
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 |
|
|
|
||
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. |
|
|
|
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
|
|
|
|
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...
|
|
|
|
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. |
|
|
|
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.... |
|
|
|
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? |
|
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
2 | Denuna | 421 | 11th December 2009 - 09:56 PM Last post by: Doug |
|||
![]() |
1 | Cymbol | 541 | 24th November 2009 - 10:07 PM Last post by: appleoddity |
|||
![]() |
0 | packerman1975 | 1,199 | 13th November 2009 - 11:30 AM Last post by: packerman1975 |
|||
![]() |
0 | sunter | 440 | 1st November 2009 - 11:11 AM Last post by: sunter |
|||
|
Time is now: 17th March 2010 - 01:05 AM |