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 / New Users / July 2007

Tip: Looking for answers? Try searching our database.

Counting columns of data that has been conditionally formatted

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 04 Jul 2007 19:55 GMT
Hi

I have twelve columns (months)of sales data (percentages calculated from
another sheet) that are conditionally formatted to highlight the highest
and lowest percentages each month (Two cells highlighted each month).

What I want to do is create two columns at the end to count the number
of times the subject has been the highest in the preceding twelve months
and the lowest in the preceding twelve months.  I need to count the
number of highlighted cells.

I have tried several formulations but have got nowhere.  I will be
grateful if anyone could help.

Regards

Mike
Charlie O'Neill - 05 Jul 2007 00:20 GMT
> Hi
>
[quoted text clipped - 13 lines]
>
> Mike

Mike

This is an answer to the problem that was posted to this side a few years
ago.  Bob Phillips and Harlan Grove developed the procedure and was posted by
Frank Kabel

Copy the procedure below and paste it into a module of the spreadsheet.  
Then use the second Sumproduct formula to find the number for the color
index.  Once the color is found then use the first sumproduct formula to
count the cells with that color.

=SUMPRODUCT(--(ColorIndex(A1:A100)=3)) to count all red cells (background
color) within the range A1:A100 or

=SUMPRODUCT(--(ColorIndex(A1:A100,TRUE)=3)) to count all red cells (font
color) within the range A1:A100

CREATING A FUNCTION TO USE IN CALUCLATIONS

To get the color index of a specific cell use =ColorIndex(A1)

------
'Code to paste in one of your modules

'---------------------------------------------------------------------
Function ColorIndex(rng As range, Optional text As Boolean = False) As Variant
'---------------------------------------------------------------------
' Function:     Returns the color index of the supplied range
' Synopsis:
' Author:       Bob Phillips/Harlan Grove
'
'---------------------------------------------------------------------
Dim cell As range, row As range
Dim i As Long, j As Long
Dim iWhite As Long, iBlack As Long
Dim aryColours As Variant

   If rng.Areas.Count > 1 Then
       ColorIndex = CVErr(xlErrValue)
       Exit Function
   End If

   iWhite = WhiteColorindex(rng.Worksheet.Parent)
   iBlack = BlackColorindex(rng.Worksheet.Parent)

   If rng.Cells.Count = 1 Then
       If text Then
           aryColours = DecodeColorIndex(rng, True, iBlack)
       Else
           aryColours = DecodeColorIndex(rng, False, iWhite)
       End If

   Else
       aryColours = rng.Value
       i = 0

       For Each row In rng.Rows
           i = i + 1
           j = 0

           For Each cell In row.Cells
               j = j + 1

               If text Then
                   aryColours(i, j) = DecodeColorIndex(cell, True, _
iBlack)
               Else
                   aryColours(i, j) = DecodeColorIndex(cell, False, _
iWhite)
               End If

           Next cell

       Next row

   End If

   ColorIndex = aryColours

End Function

Private Function WhiteColorindex(oWB As Workbook)
Dim iPalette As Long
   WhiteColorindex = 0
   For iPalette = 1 To 56
       If oWB.Colors(iPalette) = &HFFFFFF Then
           WhiteColorindex = iPalette
           Exit Function
       End If
   Next iPalette
End Function

Private Function BlackColorindex(oWB As Workbook)
Dim iPalette As Long
   BlackColorindex = 0
   For iPalette = 1 To 56
       If oWB.Colors(iPalette) = &H0 Then
           BlackColorindex = iPalette
           Exit Function
       End If
   Next iPalette
End Function

Private Function DecodeColorIndex(rng As range, text As Boolean, idx As _
Long)
Dim iColor As Long
   If text Then
       iColor = rng.Font.ColorIndex
   Else
       iColor = rng.Interior.ColorIndex
   End If
   If iColor < 0 Then
       iColor = idx
   End If
   DecodeColorIndex = iColor
End Function

> LOL
Charlie
Charlie O'Neill - 05 Jul 2007 00:28 GMT
> > Hi
> >
[quoted text clipped - 133 lines]
> > LOL
> Charlie
Charlie O'Neill - 05 Jul 2007 00:30 GMT
> > > Hi
> > >
[quoted text clipped - 133 lines]
> > > LOL
> > Charlie

Sorry I screwed up the wording on the first answer, please use the second
answer posted.

Charlie
Gord Dibben - 05 Jul 2007 02:32 GMT
Charlie

Have a read about CF colors at Chip Pearson's site.

http://www.cpearson.com/excel/CFColors.htm

Need a whole different set of functions for that.

Gord Dibben  MS Excel MVP

>> Hi
>>
[quoted text clipped - 133 lines]
>> LOL
>Charlie
Charlie O'Neill - 05 Jul 2007 02:42 GMT
I guess I don't know as much as I thought I did.  Thanks for the correction.

Charlie

> Charlie
>
[quoted text clipped - 143 lines]
> >> LOL
> >Charlie
 
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.