Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello -
I have coded up the following to automatically set the Conditional Formatting for a cell: lLine = 28 With Range("D" & lLine ) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With When I run it, the actual Formula is =D37="X" rather than =D28="X". If I do a Range().Select and then the Selection.FormatConditions.XXX it works fine. Any idea what that is all about? Am I doing something wrong here? Thanks, Joe |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Joe
You need to fully quality your range, otherwise your formulas will be relative to the activecell: Public Sub test() lLine = 28 With ActiveSheet With .Range("D" & lLine) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With End With End Sub cheers Simon "Joe HM" wrote: Hello - I have coded up the following to automatically set the Conditional Formatting for a cell: lLine = 28 With Range("D" & lLine ) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With When I run it, the actual Formula is =D37="X" rather than =D28="X". If I do a Range().Select and then the Selection.FormatConditions.XXX it works fine. Any idea what that is all about? Am I doing something wrong here? Thanks, Joe |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sorry Joe
Didn't check my last snippet properly try this: Public Sub test2() lLine = 28 With ActiveSheet With .Range("D" & lLine) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=R" & lLine & "C=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=R" & lLine & "C=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With End With End Sub It uses R1C1 instead - seems to work cheers Simon "Joe HM" wrote: Hello - I have coded up the following to automatically set the Conditional Formatting for a cell: lLine = 28 With Range("D" & lLine ) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With When I run it, the actual Formula is =D37="X" rather than =D28="X". If I do a Range().Select and then the Selection.FormatConditions.XXX it works fine. Any idea what that is all about? Am I doing something wrong here? Thanks, Joe |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Since you are using a relative formula, this will be relative to the
activecell. However, in this case, it doesn't appear that you need a relative formula: Sub AA() lLine = 28 With Range("D" & lLine) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, _ Formula1:="=$D$" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, _ Formula1:="=$D$" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With End Sub works. -- Regards, Tom Ogilvy "Joe HM" wrote in message ups.com... Hello - I have coded up the following to automatically set the Conditional Formatting for a cell: lLine = 28 With Range("D" & lLine ) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With When I run it, the actual Formula is =D37="X" rather than =D28="X". If I do a Range().Select and then the Selection.FormatConditions.XXX it works fine. Any idea what that is all about? Am I doing something wrong here? Thanks, Joe |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Is it because of the activecell. which you then change.
Try this lLine = 28 With Range("D" & lLine ) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D$" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D$" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With -- HTH RP (remove nothere from the email address if mailing direct) "Joe HM" wrote in message ups.com... Hello - I have coded up the following to automatically set the Conditional Formatting for a cell: lLine = 28 With Range("D" & lLine ) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""X""" .FormatConditions(1).Interior.ColorIndex = 3 .FormatConditions.Add Type:=xlExpression, Formula1:="=$D" & lLine & "=""Y""" .FormatConditions(2).Interior.ColorIndex = 5 End With When I run it, the actual Formula is =D37="X" rather than =D28="X". If I do a Range().Select and then the Selection.FormatConditions.XXX it works fine. Any idea what that is all about? Am I doing something wrong here? Thanks, Joe |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks!
I tried that but it gives me relative columns. I need relative rows and an absolute column. Is there a way to do that? Joe |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Make sure that the correct cell is active when you create the formula.
-- HTH RP (remove nothere from the email address if mailing direct) "Joe HM" wrote in message ups.com... Thanks! I tried that but it gives me relative columns. I need relative rows and an absolute column. Is there a way to do that? Joe |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello!
Thanks everybody so much for the help! I did activate the cell and now it's working properly! Joe |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
conditional formatting based on relative cell value | Excel Worksheet Functions | |||
Conditional Formatting with relative cells | Excel Discussion (Misc queries) | |||
Conditional Formatting Relative to adjacent cells | Excel Discussion (Misc queries) | |||
How do I set conditional formatting relative to another cell -XL20 | Excel Discussion (Misc queries) | |||
Conditional formatting changing relative when copied to other cell | Excel Worksheet Functions |