PC Review


Reply
Thread Tools Rate Thread

CountIF by Background Color

 
 
Phil H
Guest
Posts: n/a
 
      3rd Dec 2009
Need a formula to count the cells in a range with a background color yellow
(6).
 
Reply With Quote
 
 
 
 
Luke M
Guest
Posts: n/a
 
      3rd Dec 2009
You can use this short UDF called CountYellow. Note that it only counts true
background color, not format generated by conditional formatting. Detecting
conditional formatting is quite a bit more complicated...

Public Function CountYellow(r As Range) As Double
Application.Volatile = True

CountColor = 0
For Each c In r
If c.Interior.ColorIndex = 6 Then
CountColor = CountColor + 1
End If
Next

End Function
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Phil H" wrote:

> Need a formula to count the cells in a range with a background color yellow
> (6).

 
Reply With Quote
 
Luke M
Guest
Posts: n/a
 
      3rd Dec 2009
Correction, change this line:

Application.Volatile = True

to just

Application.Volatile

--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Luke M" wrote:

> You can use this short UDF called CountYellow. Note that it only counts true
> background color, not format generated by conditional formatting. Detecting
> conditional formatting is quite a bit more complicated...
>
> Public Function CountYellow(r As Range) As Double
> Application.Volatile = True
>
> CountColor = 0
> For Each c In r
> If c.Interior.ColorIndex = 6 Then
> CountColor = CountColor + 1
> End If
> Next
>
> End Function
> --
> Best Regards,
>
> Luke M
> *Remember to click "yes" if this post helped you!*
>
>
> "Phil H" wrote:
>
> > Need a formula to count the cells in a range with a background color yellow
> > (6).

 
Reply With Quote
 
Phil H
Guest
Posts: n/a
 
      3rd Dec 2009
Luke,

I put this (corrected) function code in a new module 3 in personal.xls and
used the following cell formula: =COUNTYELLOW(A2:A6935), and get a #Name?
error in the cell.

"Luke M" wrote:

> Correction, change this line:
>
> Application.Volatile = True
>
> to just
>
> Application.Volatile
>
> --
> Best Regards,
>
> Luke M
> *Remember to click "yes" if this post helped you!*
>
>
> "Luke M" wrote:
>
> > You can use this short UDF called CountYellow. Note that it only counts true
> > background color, not format generated by conditional formatting. Detecting
> > conditional formatting is quite a bit more complicated...
> >
> > Public Function CountYellow(r As Range) As Double
> > Application.Volatile = True
> >
> > CountColor = 0
> > For Each c In r
> > If c.Interior.ColorIndex = 6 Then
> > CountColor = CountColor + 1
> > End If
> > Next
> >
> > End Function
> > --
> > Best Regards,
> >
> > Luke M
> > *Remember to click "yes" if this post helped you!*
> >
> >
> > "Phil H" wrote:
> >
> > > Need a formula to count the cells in a range with a background color yellow
> > > (6).

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      3rd Dec 2009
If your color was set by conditional format, then Luke's function will not
work.


"Phil H" <(E-Mail Removed)> wrote in message
news:02975D2F-56E6-4376-8755-(E-Mail Removed)...
> Luke,
>
> I put this (corrected) function code in a new module 3 in personal.xls and
> used the following cell formula: =COUNTYELLOW(A2:A6935), and get a #Name?
> error in the cell.
>
> "Luke M" wrote:
>
>> Correction, change this line:
>>
>> Application.Volatile = True
>>
>> to just
>>
>> Application.Volatile
>>
>> --
>> Best Regards,
>>
>> Luke M
>> *Remember to click "yes" if this post helped you!*
>>
>>
>> "Luke M" wrote:
>>
>> > You can use this short UDF called CountYellow. Note that it only counts
>> > true
>> > background color, not format generated by conditional formatting.
>> > Detecting
>> > conditional formatting is quite a bit more complicated...
>> >
>> > Public Function CountYellow(r As Range) As Double
>> > Application.Volatile = True
>> >
>> > CountColor = 0
>> > For Each c In r
>> > If c.Interior.ColorIndex = 6 Then
>> > CountColor = CountColor + 1
>> > End If
>> > Next
>> >
>> > End Function
>> > --
>> > Best Regards,
>> >
>> > Luke M
>> > *Remember to click "yes" if this post helped you!*
>> >
>> >
>> > "Phil H" wrote:
>> >
>> > > Need a formula to count the cells in a range with a background color
>> > > yellow
>> > > (6).



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      3rd Dec 2009
Try:

=personal.xls!countyellow(a2:a6935)



Phil H wrote:
>
> Luke,
>
> I put this (corrected) function code in a new module 3 in personal.xls and
> used the following cell formula: =COUNTYELLOW(A2:A6935), and get a #Name?
> error in the cell.
>
> "Luke M" wrote:
>
> > Correction, change this line:
> >
> > Application.Volatile = True
> >
> > to just
> >
> > Application.Volatile
> >
> > --
> > Best Regards,
> >
> > Luke M
> > *Remember to click "yes" if this post helped you!*
> >
> >
> > "Luke M" wrote:
> >
> > > You can use this short UDF called CountYellow. Note that it only counts true
> > > background color, not format generated by conditional formatting. Detecting
> > > conditional formatting is quite a bit more complicated...
> > >
> > > Public Function CountYellow(r As Range) As Double
> > > Application.Volatile = True
> > >
> > > CountColor = 0
> > > For Each c In r
> > > If c.Interior.ColorIndex = 6 Then
> > > CountColor = CountColor + 1
> > > End If
> > > Next
> > >
> > > End Function
> > > --
> > > Best Regards,
> > >
> > > Luke M
> > > *Remember to click "yes" if this post helped you!*
> > >
> > >
> > > "Phil H" wrote:
> > >
> > > > Need a formula to count the cells in a range with a background color yellow
> > > > (6).


--

Dave Peterson
 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      3rd Dec 2009
Chip Pearson has a lot of info on counting colors at this site:

http://www.cpearson.com/excel/colors.aspx



"Phil H" <(E-Mail Removed)> wrote in message
news:02975D2F-56E6-4376-8755-(E-Mail Removed)...
> Luke,
>
> I put this (corrected) function code in a new module 3 in personal.xls and
> used the following cell formula: =COUNTYELLOW(A2:A6935), and get a #Name?
> error in the cell.
>
> "Luke M" wrote:
>
>> Correction, change this line:
>>
>> Application.Volatile = True
>>
>> to just
>>
>> Application.Volatile
>>
>> --
>> Best Regards,
>>
>> Luke M
>> *Remember to click "yes" if this post helped you!*
>>
>>
>> "Luke M" wrote:
>>
>> > You can use this short UDF called CountYellow. Note that it only counts
>> > true
>> > background color, not format generated by conditional formatting.
>> > Detecting
>> > conditional formatting is quite a bit more complicated...
>> >
>> > Public Function CountYellow(r As Range) As Double
>> > Application.Volatile = True
>> >
>> > CountColor = 0
>> > For Each c In r
>> > If c.Interior.ColorIndex = 6 Then
>> > CountColor = CountColor + 1
>> > End If
>> > Next
>> >
>> > End Function
>> > --
>> > Best Regards,
>> >
>> > Luke M
>> > *Remember to click "yes" if this post helped you!*
>> >
>> >
>> > "Phil H" wrote:
>> >
>> > > Need a formula to count the cells in a range with a background color
>> > > yellow
>> > > (6).



 
Reply With Quote
 
Phil H
Guest
Posts: n/a
 
      4th Dec 2009
All,

Color is not set by conditional formatting.

Used =personal.xls!countyellow(a2:a6935) and got a compile error – variable
not defined on the line CountColor =0, and #Value! Error in the cell.

Tried Chip’s approach, with his code, and got the #Name! error.


"Luke M" wrote:

> Correction, change this line:
>
> Application.Volatile = True
>
> to just
>
> Application.Volatile
>
> --
> Best Regards,
>
> Luke M
> *Remember to click "yes" if this post helped you!*
>
>
> "Luke M" wrote:
>
> > You can use this short UDF called CountYellow. Note that it only counts true
> > background color, not format generated by conditional formatting. Detecting
> > conditional formatting is quite a bit more complicated...
> >
> > Public Function CountYellow(r As Range) As Double
> > Application.Volatile = True
> >
> > CountColor = 0
> > For Each c In r
> > If c.Interior.ColorIndex = 6 Then
> > CountColor = CountColor + 1
> > End If
> > Next
> >
> > End Function
> > --
> > Best Regards,
> >
> > Luke M
> > *Remember to click "yes" if this post helped you!*
> >
> >
> > "Phil H" wrote:
> >
> > > Need a formula to count the cells in a range with a background color yellow
> > > (6).

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      4th Dec 2009
There is an option that you're using that forces you to declare all your
variables. CountColor is not defined.

Try this:

Option Explicit
Public Function CountYellow(r As Range) As Double
Application.Volatile True
Dim CountColor As Long
Dim c As Range

CountColor = 0
For Each c In r
If c.Interior.ColorIndex = 6 Then
CountColor = CountColor + 1
End If
Next c

'add this line, too
CountYellow = countcolor

End Function

As for the error you got from Chip's code...

You didn't put it in a general module--or you made some other typing error in
the formula. Are you sure you spelled the function name correctly (or included
the "personal.xls!" characters???)

You should give a little more info when things don't work.

Phil H wrote:
>
> All,
>
> Color is not set by conditional formatting.
>
> Used =personal.xls!countyellow(a2:a6935) and got a compile error – variable
> not defined on the line CountColor =0, and #Value! Error in the cell.
>
> Tried Chip’s approach, with his code, and got the #Name! error.
>
> "Luke M" wrote:
>
> > Correction, change this line:
> >
> > Application.Volatile = True
> >
> > to just
> >
> > Application.Volatile
> >
> > --
> > Best Regards,
> >
> > Luke M
> > *Remember to click "yes" if this post helped you!*
> >
> >
> > "Luke M" wrote:
> >
> > > You can use this short UDF called CountYellow. Note that it only counts true
> > > background color, not format generated by conditional formatting. Detecting
> > > conditional formatting is quite a bit more complicated...
> > >
> > > Public Function CountYellow(r As Range) As Double
> > > Application.Volatile = True
> > >
> > > CountColor = 0
> > > For Each c In r
> > > If c.Interior.ColorIndex = 6 Then
> > > CountColor = CountColor + 1
> > > End If
> > > Next
> > >
> > > End Function
> > > --
> > > Best Regards,
> > >
> > > Luke M
> > > *Remember to click "yes" if this post helped you!*
> > >
> > >
> > > "Phil H" wrote:
> > >
> > > > Need a formula to count the cells in a range with a background color yellow
> > > > (6).


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom color for font color and or background shading color Ben Microsoft Excel Programming 2 5th May 2010 02:32 PM
Checkbox to change background color, font color and remove/ add bo Sara Microsoft Excel Misc 1 11th Nov 2008 08:17 PM
How do I change the background color and font color in word docum MarieInLaguna Microsoft Word Document Management 1 13th Aug 2008 05:02 PM
Default Border, Font Color, and Cell Background Color Elijah Microsoft Excel Misc 1 28th Oct 2005 04:10 PM
The BACKGROUND COLOR OF THE COMMAND BUTTON FACES RESET TO THE DEFAULT COLOR Alva Dean Microsoft Excel Programming 2 8th Mar 2004 08:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:59 AM.