ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Conditional Formatting Bug w/ relative formula? (https://www.excelbanter.com/excel-programming/325417-conditional-formatting-bug-w-relative-formula.html)

Joe HM

Conditional Formatting Bug w/ relative formula?
 
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


Simon Murphy[_4_]

Conditional Formatting Bug w/ relative formula?
 
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



Simon Murphy[_4_]

part 2 Conditional Formatting Bug w/ relative formula?
 
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



Tom Ogilvy

Conditional Formatting Bug w/ relative formula?
 
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




Bob Phillips[_6_]

Conditional Formatting Bug w/ relative formula?
 
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




Joe HM

part 2 Conditional Formatting Bug w/ relative formula?
 
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


Bob Phillips[_6_]

part 2 Conditional Formatting Bug w/ relative formula?
 
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




Joe HM

Conditional Formatting Bug w/ relative formula?
 
Hello!

Thanks everybody so much for the help! I did activate the cell and now
it's working properly!

Joe



All times are GMT +1. The time now is 03:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com