[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Count number of grey shaded cells

Posted on 2011-02-21
18
Medium Priority
?
1,321 Views
Last Modified: 2012-05-11
Dear Experts:

I would like to count the number of cells that have a grey cell shading (RGB 223, 223, 223). They are all located in a row but are not contiguous.

I have attached a sample file for your convenience.

Regards, Andreas  Count-number-grey-shaded-cells.xlsx
0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 5
  • +2
18 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34943806
Here's a UDF:
Function CountGreyCells(rngIn As Range) As Long
   Dim rngCell As Range
   Dim lngTemp As Long
   For Each rngCell In rngIn
      If rngCell.Interior.Color = RGB(223, 223, 223) Then lngTemp = lngTemp + 1
   Next rngCell
   CountGreyCells = lngTemp
End Function

Open in new window

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34943813
In K8 you would enter:
=countgreycells(B8:J8)
0
 
LVL 18

Assisted Solution

by:Cory Vandenberg
Cory Vandenberg earned 400 total points
ID: 34943823
Have a look here:

Chip Pearson's Color Functions

With the use of that module, you can count the number of cell's with a certain fill color.  The color will need to be in your palette.

I really can't say it any better than Chip has already done on his site, so I suggest going there and reading, but my guess is you will either use his CountColor function or a SUMPRODUCT such as

=SUMPRODUCT(--(COLORINDEXOFRANGE(B11:B17,FALSE,1)=COLORINDEXOFONECELL(H7,FALSE,1)))

where you will compare the range against the color of a certain cell.  There are a few ways to do what you want with his color functions.  It is really up to you how to do it.

Since you will need macro code, you will need to save the workbook as a macro-enabled workbook.

WC
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 400 total points
ID: 34943840
The colour index of that grey is 15 so you can use this code.

Sub sample()
    Dim ws As Worksheet
    Dim rng As Range, cl As Range
    Dim nCount As Long
    
    Set ws = Sheets("Tabelle1")
    Set rng = ws.Range("A8:K8")
    
    For Each cl In rng
        If cl.Interior.ColorIndex = 15 Then
            nCount = nCount + 1
        End If
    Next
    
    Debug.Print nCount
End Sub

Open in new window


Sid
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34943866
Oh, and here's your file with UDF in place. Had to convert to xlsm of course.
Count-number-grey-shaded-cells.xlsm
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1200 total points
ID: 34943877
Forgot the volatility you will need - call it with:
=countgreycells(B8:J8)+NOW()*0
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 34944009
Nice seeing you back, WC :)
0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 34944117
Thanks Patrick,

I have been busy the past year coding up automated reports from Excel to Word.  My analytics group was bought by another company last fall and we are just getting in our new building.  I will probably be spending the majority of my time this year coding in SAS, but I have some opportunity to do some things in Office related work.  I'm having to make the jump to Office 07/10, so I'm learning some new stuff too.  Already done some work with customizing the Office Ribbon for some Add-Ins of mine.

You probably won't see much of me around here, but I shouldn't be a ghost like the last 6 months or so.

Cheers,
WC
0
 

Author Comment

by:AndreasHermle
ID: 34944818
Dear all,

thank you very much for the overwhelming support, especially the one from rorya.

There is one question I am having RORYA:
What do mean by 'forgot the volatility you will need'?

WC: looks awesome, will test it and get back to you.

Will try every solution / approach and report back to you

Regards and thank you again.

Andreas
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34944898
You need to make sure the function gets recalculated when the worksheet does because changing cell colours does not cause a recalculation.
As a general observation, colour should be used to highlight data, not used as data.
0
 

Author Comment

by:AndreasHermle
ID: 34948701
Hi rorya:

just tried it out. Works like a charm. Great stuff. Thank you very much for your superb support.

Regards, Andreas
0
 

Author Comment

by:AndreasHermle
ID: 34948712
Hi Sid,

is it possible to re-write your code so that instead of being hardcoded, the current selection on the current worksheet gets worked on:

Set ws = Sheets("Tabelle1")
    Set rng = ws.Range("A8:K8")

Help is much appreciated. Thank you very much in advance. Regards, Andreas
0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 34952173
No points for this.  Just adjusting Sid's code to your request.  Notice it is exactly like Rory's function.  

WC
Function sample(rng as Range) as Long
    Dim cl As Range
    Dim nCount As Long
    
    For Each cl In rng
        If cl.Interior.ColorIndex = 15 Then nCount = nCount + 1
    Next
    sample = nCount
End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 34952196
Whoops, forgot to change the End Sub to an End Function.

:)  It's early, need to wake up.
0
 

Author Comment

by:AndreasHermle
ID: 34953409
HI WarCrimes:

ok, thank you very much for your great support.

Regards, Andreas
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34953798
Thanks WarCrimes :)

Was away so couldn't do the needful in this thread :)

Sid
0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 34960787
No worries,

We are all here to help.  Glad I can step in now and then.

Cheers,
WC
0
 

Author Closing Comment

by:AndreasHermle
ID: 34961518
Dear All,

again thank you very much for your overwhelming support. It is kind of hard to distribute the points equally.

This forum really deserves its name.

Regards, Andreas
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question