Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Counting colored cells

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Niall 84 - 20 May 2008 14:13 GMT
I got this code from a site on-line.

I need to scan a list of cells in a worksheet and search for any cells
that are colored red.

If any of the cells are colored red I need to display it in a separate
worksheet that deals with a summery of all the work so far. It would
be handy, although not essential if the number of red cells on a sheet
was given.

I think this code will do it apart from one problem. My current red
cells are red due to conditional formatting, I think that this is
causing problems.

Any ideas?

I am not too advanced with VBA so please be patient with me!!!

Thanks,

Niall.

v

code:

Function CountColour(Rng As Range, ColourMatch As Integer,
BackgroundOrFont As String)
'Rng is the set of cells to be checked
'ColurMatch is the Color INDEX of the colour being tested for
'BackgroundOrFont requires an "F" or "B" to indicate whether to test
Font or Background colour

Dim c As Range, TempStore As Long

TempStore = 0

Select Case BackgroundOrFont
   Case "B"
       For Each c In Rng
           If c.Interior.ColorIndex = ColourMatch Then
               TempStore = TempStore + 1
           End If
       Next
   Case "F"
       For Each c In Rng
           If c.Font.ColorIndex = ColourMatch Then
               TempStore = TempStore + 1
           End If
       Next
   Case Else
       CountColour = "Choose F or B only"
       Exit Function

End Select

CountColour = TempStore

End Function
Bob Phillips - 20 May 2008 14:49 GMT
See http://www.xldynamic.com/source/xld.CFConditions.html

Signature

HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

>I got this code from a site on-line.
>
[quoted text clipped - 55 lines]
>
> End Function
Niall 84 - 20 May 2008 15:23 GMT
Thank you very much,

Niall.
JLGWhiz - 20 May 2008 15:52 GMT
Don't know if this will help, but I set up a range from A1 to C12 and
put some conditional formatted cells in the range for red interior.
This macro counted them accurately.

Sub countColor()
  Dim c As Range
  Set myRange = Sheets(1).Range("A1:C12")
  cntr = 0
  For Each c In myRange
    If c.FormatConditions.Count > 0 Then
      If c.FormatConditions(1).Interior.ColorIndex = 3 Then
        cntr = cntr + 1
      End If
    End If
  Next
  MsgBox cntr
End Sub

> Thank you very much,
>
> Niall.
JLGWhiz - 20 May 2008 16:13 GMT
One problem with counting conditional format colored cells is that the cell
might get counted even if the criteria is not met to set the color.  The code
counts the FormatCoditions with a Interior.ColorIndex = 3, not the actual
interior color.
In other words, it does not consider if the criteria is met that would
actually color the interior.

> Thank you very much,
>
> Niall.
Niall 84 - 20 May 2008 16:34 GMT
Thanks for your help,

Much appreciated.

Niall.
Bob Phillips - 21 May 2008 11:54 GMT
See the link I provided for a way to handle this.

Signature

HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

> One problem with counting conditional format colored cells is that the
> cell
[quoted text clipped - 8 lines]
>>
>> Niall.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.