PC Review


Reply
Thread Tools Rate Thread

Conditional Computing

 
 
aung@sinaptiq.com
Guest
Posts: n/a
 
      12th Dec 2006
Hi,

I am not sure Excel 2003 can do this or not. Here is what I would like
to do.
I have a column (say column A) with some numbers. Some of the cells are
colored green and some are colored red.
I would like to add all the cells colored in red at cell B1 and add all
the cells colored in green at cell C1. The color may be changed
dynamically by means of conditional formatting.

Any idea?

Thanks.

 
Reply With Quote
 
 
 
 
John Coleman
Guest
Posts: n/a
 
      12th Dec 2006
Hi,

You can do this in VBA of course (hint: use the worksheet_calculate
event). But - before you go that rout, have you considered using the
conditional sum wizard? It is designed to create conditional sum
formulas using the same sorts of criteria as are used in conditional
formatting. From your problem description, this seems like a good
possibility. It is an add in. Go to tools -> add ins and install it if
it isn't already. This may be better than a VBA approach since, for
example, the code might break if you decide you want to use different
colors in the conditional formatting. It would probably also be quicker
if you are talking about a large number of cells.

HTH

-John Coleman

(E-Mail Removed) wrote:
> Hi,
>
> I am not sure Excel 2003 can do this or not. Here is what I would like
> to do.
> I have a column (say column A) with some numbers. Some of the cells are
> colored green and some are colored red.
> I would like to add all the cells colored in red at cell B1 and add all
> the cells colored in green at cell C1. The color may be changed
> dynamically by means of conditional formatting.
>
> Any idea?
>
> Thanks.


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      12th Dec 2006
SUM them based upon the same condition that is applied to CF.

For instance, if the CF is greater than 10, then use

=SUMIF(A1:A10,">10")

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I am not sure Excel 2003 can do this or not. Here is what I would like
> to do.
> I have a column (say column A) with some numbers. Some of the cells are
> colored green and some are colored red.
> I would like to add all the cells colored in red at cell B1 and add all
> the cells colored in green at cell C1. The color may be changed
> dynamically by means of conditional formatting.
>
> Any idea?
>
> Thanks.
>



 
Reply With Quote
 
NickH
Guest
Posts: n/a
 
      12th Dec 2006
I think John's probably right, especially if you are already using
Excel's built-in Conditional Formatting.

However, should you decide to go the VBA route the following functions
may be of some help.

NB. These functions will NOT detect a cell interior colour that has
been set using conditional formatting. To do that you would need to
write code to interrogate the condtional format settings, in which case
you might as well write your own conditional formatting routine (yeah,
listen to John).

Still reading? Okay go here for some Conditional Format macro
suggestions...

http://www.mvps.org/dmcritchie/excel/condfmt.htm

Here's a function to do the summing...

Public Function ColorSum(mRng As Range, mColor As Integer) As Single
Dim mTot As Single
Dim c As Range


For Each c In mRng
If IsNumeric(c.Value) Then
If c.Interior.ColorIndex = mColor Then
mTot = mTot + c.Value
End If
End If
Next c

ColorSum = mTot

End Function

To use enter something like =ColorSum(A1:A20,10) into a cell.

And here's a function to return the interior colour index of a cell...

Public Function GetColorIndex(mCell As Range) As Integer
''' Quick check to find the interior color of a cell. _
If multiple cells selected only top left examined

GetColorIndex = mCell.Range("A1").Interior.ColorIndex
End Function

To use enter something like =GetColorIndex(A3) into a cell.


Hope you listened to John - I'm just killing time. ?;^)

NickH

 
Reply With Quote
 
John Coleman
Guest
Posts: n/a
 
      12th Dec 2006
Bob,

Your advice is probably the way to go, but do you have any idea why the
following doesn't work as intended?

Function ColorSum(R As Range, i As Long) As Variant
Dim sum As Variant
Dim cl As Range
For Each cl In R.Cells
If cl.Interior.ColorIndex = i Then
sum = sum + cl.Value
End If
Next cl
ColorSum = sum
End Function

Sub RedGreenSums()
Dim R As Range
Set R = Range("A1", Cells(Rows.Count, 1).End(xlUp))
Range("B1").Value = ColorSum(R, 3)
Range("C1").Value = ColorSum(R, 10)
End Sub

Private Sub Worksheet_Calculate()
RedGreenSums
End Sub

'For debugging purposes:

Sub ShowIndex()
On Error Resume Next
MsgBox Selection.Interior.ColorIndex
End Sub


It works fine if the colors are manually set. But - if they are set by
conditional formatting then wierd things happen. I set up a trial sheet
in which the first 10 cells of column A were colored green for positive
values and red for negative values. When I actually change the values
from positive to negative or vice versa I get (when I run the ShowIndex
sub on various cells) that all colorindices are -4142. Maybe that is
some alias for xlAutomatic or something like that and the conditional
formatting in effect doesn't change the color per se but changes the
meaning of automatic for that cell. But then - this raises the
question: how can you determine the color of a cell if its color has
been set by conditional formatting? I guess you would have to determine
programmatically which condition applies and go inside the
corresponding formatcondition object - which seems like a lot of effort
to read what should be an easy property to read off.

-John Coleman

Bob Phillips wrote:
> SUM them based upon the same condition that is applied to CF.
>
> For instance, if the CF is greater than 10, then use
>
> =SUMIF(A1:A10,">10")
>
> --
> ---
> HTH
>
> Bob
>
> (change the xxxx to gmail if mailing direct)
>
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi,
> >
> > I am not sure Excel 2003 can do this or not. Here is what I would like
> > to do.
> > I have a column (say column A) with some numbers. Some of the cells are
> > colored green and some are colored red.
> > I would like to add all the cells colored in red at cell B1 and add all
> > the cells colored in green at cell C1. The color may be changed
> > dynamically by means of conditional formatting.
> >
> > Any idea?
> >
> > Thanks.
> >


 
Reply With Quote
 
John Coleman
Guest
Posts: n/a
 
      12th Dec 2006
A few seconds after I posted I realized that it is obvious that
conditional formatting shouldn't change the colorindex - this allows
the original colorindex to stick around as a default color when none of
the conditions apply. It would be nice if cells had a
DisplayedColorIndex property in addition to a ColorIndex one. I'll have
to write my own function. No need to answer my previous post.

John Coleman wrote:
> Bob,
>
> Your advice is probably the way to go, but do you have any idea why the
> following doesn't work as intended?
>
> Function ColorSum(R As Range, i As Long) As Variant
> Dim sum As Variant
> Dim cl As Range
> For Each cl In R.Cells
> If cl.Interior.ColorIndex = i Then
> sum = sum + cl.Value
> End If
> Next cl
> ColorSum = sum
> End Function
>
> Sub RedGreenSums()
> Dim R As Range
> Set R = Range("A1", Cells(Rows.Count, 1).End(xlUp))
> Range("B1").Value = ColorSum(R, 3)
> Range("C1").Value = ColorSum(R, 10)
> End Sub
>
> Private Sub Worksheet_Calculate()
> RedGreenSums
> End Sub
>
> 'For debugging purposes:
>
> Sub ShowIndex()
> On Error Resume Next
> MsgBox Selection.Interior.ColorIndex
> End Sub
>
>
> It works fine if the colors are manually set. But - if they are set by
> conditional formatting then wierd things happen. I set up a trial sheet
> in which the first 10 cells of column A were colored green for positive
> values and red for negative values. When I actually change the values
> from positive to negative or vice versa I get (when I run the ShowIndex
> sub on various cells) that all colorindices are -4142. Maybe that is
> some alias for xlAutomatic or something like that and the conditional
> formatting in effect doesn't change the color per se but changes the
> meaning of automatic for that cell. But then - this raises the
> question: how can you determine the color of a cell if its color has
> been set by conditional formatting? I guess you would have to determine
> programmatically which condition applies and go inside the
> corresponding formatcondition object - which seems like a lot of effort
> to read what should be an easy property to read off.
>
> -John Coleman
>
> Bob Phillips wrote:
> > SUM them based upon the same condition that is applied to CF.
> >
> > For instance, if the CF is greater than 10, then use
> >
> > =SUMIF(A1:A10,">10")
> >
> > --
> > ---
> > HTH
> >
> > Bob
> >
> > (change the xxxx to gmail if mailing direct)
> >
> >
> > <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Hi,
> > >
> > > I am not sure Excel 2003 can do this or not. Here is what I would like
> > > to do.
> > > I have a column (say column A) with some numbers. Some of the cells are
> > > colored green and some are colored red.
> > > I would like to add all the cells colored in red at cell B1 and add all
> > > the cells colored in green at cell C1. The color may be changed
> > > dynamically by means of conditional formatting.
> > >
> > > Any idea?
> > >
> > > Thanks.
> > >


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      12th Dec 2006
John,

They do, that is what the CF colour is. It is difficult, but not impossible
to get the CF colour. See http://xldynamic.com/source/xld.CFConditions.html

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"John Coleman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>A few seconds after I posted I realized that it is obvious that
> conditional formatting shouldn't change the colorindex - this allows
> the original colorindex to stick around as a default color when none of
> the conditions apply. It would be nice if cells had a
> DisplayedColorIndex property in addition to a ColorIndex one. I'll have
> to write my own function. No need to answer my previous post.
>
> John Coleman wrote:
>> Bob,
>>
>> Your advice is probably the way to go, but do you have any idea why the
>> following doesn't work as intended?
>>
>> Function ColorSum(R As Range, i As Long) As Variant
>> Dim sum As Variant
>> Dim cl As Range
>> For Each cl In R.Cells
>> If cl.Interior.ColorIndex = i Then
>> sum = sum + cl.Value
>> End If
>> Next cl
>> ColorSum = sum
>> End Function
>>
>> Sub RedGreenSums()
>> Dim R As Range
>> Set R = Range("A1", Cells(Rows.Count, 1).End(xlUp))
>> Range("B1").Value = ColorSum(R, 3)
>> Range("C1").Value = ColorSum(R, 10)
>> End Sub
>>
>> Private Sub Worksheet_Calculate()
>> RedGreenSums
>> End Sub
>>
>> 'For debugging purposes:
>>
>> Sub ShowIndex()
>> On Error Resume Next
>> MsgBox Selection.Interior.ColorIndex
>> End Sub
>>
>>
>> It works fine if the colors are manually set. But - if they are set by
>> conditional formatting then wierd things happen. I set up a trial sheet
>> in which the first 10 cells of column A were colored green for positive
>> values and red for negative values. When I actually change the values
>> from positive to negative or vice versa I get (when I run the ShowIndex
>> sub on various cells) that all colorindices are -4142. Maybe that is
>> some alias for xlAutomatic or something like that and the conditional
>> formatting in effect doesn't change the color per se but changes the
>> meaning of automatic for that cell. But then - this raises the
>> question: how can you determine the color of a cell if its color has
>> been set by conditional formatting? I guess you would have to determine
>> programmatically which condition applies and go inside the
>> corresponding formatcondition object - which seems like a lot of effort
>> to read what should be an easy property to read off.
>>
>> -John Coleman
>>
>> Bob Phillips wrote:
>> > SUM them based upon the same condition that is applied to CF.
>> >
>> > For instance, if the CF is greater than 10, then use
>> >
>> > =SUMIF(A1:A10,">10")
>> >
>> > --
>> > ---
>> > HTH
>> >
>> > Bob
>> >
>> > (change the xxxx to gmail if mailing direct)
>> >
>> >
>> > <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> > > Hi,
>> > >
>> > > I am not sure Excel 2003 can do this or not. Here is what I would
>> > > like
>> > > to do.
>> > > I have a column (say column A) with some numbers. Some of the cells
>> > > are
>> > > colored green and some are colored red.
>> > > I would like to add all the cells colored in red at cell B1 and add
>> > > all
>> > > the cells colored in green at cell C1. The color may be changed
>> > > dynamically by means of conditional formatting.
>> > >
>> > > Any idea?
>> > >
>> > > Thanks.
>> > >

>



 
Reply With Quote
 
John Coleman
Guest
Posts: n/a
 
      12th Dec 2006
Bob,

Impressive. I find it amazing how involved the code is. Conditional
formats ultimately involve a simple boolean value that the application
has stored *somewhere*, and given how important conditional formatting
is in many spreadsheets it is disappointing that this value is not
exposed in the object model. You are to be commended in being able to
discover a work-around. Thanks for the link!

-John Coleman

Bob Phillips wrote:
> John,
>
> They do, that is what the CF colour is. It is difficult, but not impossible
> to get the CF colour. See http://xldynamic.com/source/xld.CFConditions.html
>
> --
> ---
> HTH
>
> Bob
>
> (change the xxxx to gmail if mailing direct)
>
>
> "John Coleman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >A few seconds after I posted I realized that it is obvious that
> > conditional formatting shouldn't change the colorindex - this allows
> > the original colorindex to stick around as a default color when none of
> > the conditions apply. It would be nice if cells had a
> > DisplayedColorIndex property in addition to a ColorIndex one. I'll have
> > to write my own function. No need to answer my previous post.
> >
> > John Coleman wrote:
> >> Bob,
> >>
> >> Your advice is probably the way to go, but do you have any idea why the
> >> following doesn't work as intended?
> >>
> >> Function ColorSum(R As Range, i As Long) As Variant
> >> Dim sum As Variant
> >> Dim cl As Range
> >> For Each cl In R.Cells
> >> If cl.Interior.ColorIndex = i Then
> >> sum = sum + cl.Value
> >> End If
> >> Next cl
> >> ColorSum = sum
> >> End Function
> >>
> >> Sub RedGreenSums()
> >> Dim R As Range
> >> Set R = Range("A1", Cells(Rows.Count, 1).End(xlUp))
> >> Range("B1").Value = ColorSum(R, 3)
> >> Range("C1").Value = ColorSum(R, 10)
> >> End Sub
> >>
> >> Private Sub Worksheet_Calculate()
> >> RedGreenSums
> >> End Sub
> >>
> >> 'For debugging purposes:
> >>
> >> Sub ShowIndex()
> >> On Error Resume Next
> >> MsgBox Selection.Interior.ColorIndex
> >> End Sub
> >>
> >>
> >> It works fine if the colors are manually set. But - if they are set by
> >> conditional formatting then wierd things happen. I set up a trial sheet
> >> in which the first 10 cells of column A were colored green for positive
> >> values and red for negative values. When I actually change the values
> >> from positive to negative or vice versa I get (when I run the ShowIndex
> >> sub on various cells) that all colorindices are -4142. Maybe that is
> >> some alias for xlAutomatic or something like that and the conditional
> >> formatting in effect doesn't change the color per se but changes the
> >> meaning of automatic for that cell. But then - this raises the
> >> question: how can you determine the color of a cell if its color has
> >> been set by conditional formatting? I guess you would have to determine
> >> programmatically which condition applies and go inside the
> >> corresponding formatcondition object - which seems like a lot of effort
> >> to read what should be an easy property to read off.
> >>
> >> -John Coleman
> >>
> >> Bob Phillips wrote:
> >> > SUM them based upon the same condition that is applied to CF.
> >> >
> >> > For instance, if the CF is greater than 10, then use
> >> >
> >> > =SUMIF(A1:A10,">10")
> >> >
> >> > --
> >> > ---
> >> > HTH
> >> >
> >> > Bob
> >> >
> >> > (change the xxxx to gmail if mailing direct)
> >> >
> >> >
> >> > <(E-Mail Removed)> wrote in message
> >> > news:(E-Mail Removed)...
> >> > > Hi,
> >> > >
> >> > > I am not sure Excel 2003 can do this or not. Here is what I would
> >> > > like
> >> > > to do.
> >> > > I have a column (say column A) with some numbers. Some of the cells
> >> > > are
> >> > > colored green and some are colored red.
> >> > > I would like to add all the cells colored in red at cell B1 and add
> >> > > all
> >> > > the cells colored in green at cell C1. The color may be changed
> >> > > dynamically by means of conditional formatting.
> >> > >
> >> > > Any idea?
> >> > >
> >> > > Thanks.
> >> > >

> >


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      12th Dec 2006
John,

The big problem lies in what is actually a feature of CF. If you select a
range of cells and apply CF, it will adjust any formula presented relative
to the position of the cell in the selection. This is a very convenient way
of selecting a range and using say

=AND(A1>B1,C1>TODAY())

next cell will adjust the row or column as appropriate. As I said, it is
very useful as it allows you to setup multiple cells at once, but the CF is
relative. And when you are not in that cell, you have to make an adjustment
for where the CF is being evaluated from. Therein lies the difficulty, and
thus the coding complexity.

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"John Coleman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bob,
>
> Impressive. I find it amazing how involved the code is. Conditional
> formats ultimately involve a simple boolean value that the application
> has stored *somewhere*, and given how important conditional formatting
> is in many spreadsheets it is disappointing that this value is not
> exposed in the object model. You are to be commended in being able to
> discover a work-around. Thanks for the link!
>
> -John Coleman
>
> Bob Phillips wrote:
>> John,
>>
>> They do, that is what the CF colour is. It is difficult, but not
>> impossible
>> to get the CF colour. See
>> http://xldynamic.com/source/xld.CFConditions.html
>>
>> --
>> ---
>> HTH
>>
>> Bob
>>
>> (change the xxxx to gmail if mailing direct)
>>
>>
>> "John Coleman" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >A few seconds after I posted I realized that it is obvious that
>> > conditional formatting shouldn't change the colorindex - this allows
>> > the original colorindex to stick around as a default color when none of
>> > the conditions apply. It would be nice if cells had a
>> > DisplayedColorIndex property in addition to a ColorIndex one. I'll have
>> > to write my own function. No need to answer my previous post.
>> >
>> > John Coleman wrote:
>> >> Bob,
>> >>
>> >> Your advice is probably the way to go, but do you have any idea why
>> >> the
>> >> following doesn't work as intended?
>> >>
>> >> Function ColorSum(R As Range, i As Long) As Variant
>> >> Dim sum As Variant
>> >> Dim cl As Range
>> >> For Each cl In R.Cells
>> >> If cl.Interior.ColorIndex = i Then
>> >> sum = sum + cl.Value
>> >> End If
>> >> Next cl
>> >> ColorSum = sum
>> >> End Function
>> >>
>> >> Sub RedGreenSums()
>> >> Dim R As Range
>> >> Set R = Range("A1", Cells(Rows.Count, 1).End(xlUp))
>> >> Range("B1").Value = ColorSum(R, 3)
>> >> Range("C1").Value = ColorSum(R, 10)
>> >> End Sub
>> >>
>> >> Private Sub Worksheet_Calculate()
>> >> RedGreenSums
>> >> End Sub
>> >>
>> >> 'For debugging purposes:
>> >>
>> >> Sub ShowIndex()
>> >> On Error Resume Next
>> >> MsgBox Selection.Interior.ColorIndex
>> >> End Sub
>> >>
>> >>
>> >> It works fine if the colors are manually set. But - if they are set by
>> >> conditional formatting then wierd things happen. I set up a trial
>> >> sheet
>> >> in which the first 10 cells of column A were colored green for
>> >> positive
>> >> values and red for negative values. When I actually change the values
>> >> from positive to negative or vice versa I get (when I run the
>> >> ShowIndex
>> >> sub on various cells) that all colorindices are -4142. Maybe that is
>> >> some alias for xlAutomatic or something like that and the conditional
>> >> formatting in effect doesn't change the color per se but changes the
>> >> meaning of automatic for that cell. But then - this raises the
>> >> question: how can you determine the color of a cell if its color has
>> >> been set by conditional formatting? I guess you would have to
>> >> determine
>> >> programmatically which condition applies and go inside the
>> >> corresponding formatcondition object - which seems like a lot of
>> >> effort
>> >> to read what should be an easy property to read off.
>> >>
>> >> -John Coleman
>> >>
>> >> Bob Phillips wrote:
>> >> > SUM them based upon the same condition that is applied to CF.
>> >> >
>> >> > For instance, if the CF is greater than 10, then use
>> >> >
>> >> > =SUMIF(A1:A10,">10")
>> >> >
>> >> > --
>> >> > ---
>> >> > HTH
>> >> >
>> >> > Bob
>> >> >
>> >> > (change the xxxx to gmail if mailing direct)
>> >> >
>> >> >
>> >> > <(E-Mail Removed)> wrote in message
>> >> > news:(E-Mail Removed)...
>> >> > > Hi,
>> >> > >
>> >> > > I am not sure Excel 2003 can do this or not. Here is what I would
>> >> > > like
>> >> > > to do.
>> >> > > I have a column (say column A) with some numbers. Some of the
>> >> > > cells
>> >> > > are
>> >> > > colored green and some are colored red.
>> >> > > I would like to add all the cells colored in red at cell B1 and
>> >> > > add
>> >> > > all
>> >> > > the cells colored in green at cell C1. The color may be changed
>> >> > > dynamically by means of conditional formatting.
>> >> > >
>> >> > > Any idea?
>> >> > >
>> >> > > Thanks.
>> >> > >
>> >

>



 
Reply With Quote
 
NickH
Guest
Posts: n/a
 
      12th Dec 2006
Very useful stuff Bob, Thanks.

NickH

 
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
Conditional Computing aung@sinaptiq.com Microsoft Excel Worksheet Functions 1 12th Dec 2006 02:49 PM
64 bit computing =?Utf-8?B?TmFkYXY=?= Microsoft VC .NET 6 25th Apr 2005 02:37 PM
Alt F4 Computing Alt F4 AMD 64 Bit 0 4th May 2004 05:08 PM
Alt F4 Computing Alt F4 Computer Hardware 0 4th May 2004 05:08 PM
computing age barbara Microsoft Access Getting Started 1 11th Aug 2003 08:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:32 AM.