View Full Version : Using the answer from a formula


philipwaldram
08-27-2008, 10:08 AM
Hi
I'm trying to use a piece of code to change the colour of a cell depending on the answer from that cells formula.
E.g. =a1+b1+c1 in cell d1
If the answer to the formula in d1 is 1 the cell colour needs to be red, if the answer is 2 the colour needs to be yellow and any other numbers green.

If Range("d1") = 1 Then
Selection.Interior.ColorIndex = 3
ElseIf Range("d1 = 2 Then
Selection.Interior.ColorIndex = 6
Else
Selection.Font.ColorIndex = 4
Selection.Interior.ColorIndex = 4
End If

But this doesn't work with the calculation. It only works when the number is typed in the cell.
Is is something like:
Range("d1").value

Thanks
Phil

ajetrumpet
08-27-2008, 08:24 PM
you don't need code to do this. The conditional formatting wizard will accomplish it. Click on the cell that contains the formula, and go to format > conditional formatting. Put as many conditions as you want in there.

shades
08-28-2008, 10:11 AM
Well, up to three conditions. :D

Anything more and then you need code.

philipwaldram
08-28-2008, 10:28 AM
Thanks, thats made it a lot easier :-)

philipwaldram
08-28-2008, 01:11 PM
I've now come across another problem and need to use the value created by a formula in the code. How would I refer to this value?
Thanks Phil

Vassago
08-28-2008, 04:02 PM
I've now come across another problem and need to use the value created by a formula in the code. How would I refer to this value?
Thanks Phil

Conditional formatting should already be using values of the formula's to determine the format of the cell. What do you have for selected and typed for your conditional formats?

ajetrumpet
08-28-2008, 09:54 PM
I've now come across another problem and need to use the value created by a formula in the code. How would I refer to this value?
Thanks PhilYou should reference the cell. The value is automatically captured. Something like:range("cell number")should work fine.