Conditional Formatting applied using VBA results in incorrect reference

DJAXE

New Member
Joined
Feb 15, 2011
Messages
29
The search I ran for "conditional formatting" produced a lot of threads with an unhelpful title, so I've crammed as much detail as I can into the heading.

This is not a question, rather I'm just posting it in case someone else comes up with the same problem. I've found one other reference to this issue on the 'net, but it was unresolved.

Basically, I had a piece of code which was applying Conditional Formatting to Excel cells (which was based on a Formula) but it was changing the row references.

I even tried recording a macro to get the result I wanted, but it still failed.

For example: I recorded the following macro (unnecessary lines deleted):

Code:
Sub Macro1()
    Range("A1").Select
    Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B3=1"
    Selection.FormatConditions(1).Interior.ColorIndex = 3
End Sub

In the Conditional Formatting dialog for cell A1 I'd have the same formula, but the row number was offset by 3, upwards (kind of) and produced:

=B65533=1

It was truly bizarre and seemingly no matter what I did or what combinations I tried, I got the same result. Regardless of which cell I applied the formatting to, the row in the resulting formula was three rows above the one I specified.

Anyway, the resolution appears to be simply to re-start Excel because all that happened yesterday and now it's not happening.

Very strange.

Version of Excel: 2003 SP3 11.8332.8333
VBA Version 6.5.1053

Again, this is not a question - although if anyone else has seen something similar and got a more technical answer/fix than mine, I'd love to hear about it.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It works for me...

But try not using Select and Selection.
You never know when "Selection" is not necessarily what you think it is.

Try working with the range directly..

Code:
Sub Macro1()
With Range("A1")
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=B3=1"
    .FormatConditions(1).Interior.ColorIndex = 3
End With
End Sub
 
Upvote 0
Thanks for the response, but I wasn't actually asking for assistance, the code was just an example of something I tried.
 
Upvote 0
I'm confused - do you want a fix or not?
 
Upvote 0
Sorry, I thought I was pretty clear in my original post.

Again, this is not a question - although if anyone else has seen something similar and got a more technical answer/fix than mine, I'd love to hear about it.

My "fix" for the offset error I encountered amounted to restarting Excel.

The sequence of events was:
1. I wrote some code to apply conditional formatting (which was referring to the cell directly, not a selection).
2. The conditional formatting ended up with a formula offset by 3 rows.
3. I tried recording a macro to see if it was Excel or something in my code (example in original post), but it produced the same result.
4. The next day, the problem had disappeared.
5. I posted the problem/resolution here because I found only one other reference to it on the 'net but with no "fix".
 
Upvote 0
I apologise if I misinterpreted the post.:oops:

I've definitely seen something similar when using code to set conditional formatting involving a formula.

I found something that worked for me but I've no idea if you would call it a 'fix' exactly.

What I found was that if you converted the formula to use R1C1 notation the code worked first time.

The references in the formula were correct, the conditional formatting worked as it should...

The hardest part, for me anyway, was the conversion of the formula from A1 to R1C1 notation.

Is that any use?:)

PS If you can explain the logic of the conditional formatting you are trying to apply I'd be happy to post some code.
 
Upvote 0
That's a good idea, Norie. At the time, I just put it aside because it wasn't an essential part of the functionality.

I had to convert my range names from a Row/Cell number to create the range address for the Conditional Formatting, so there'd be no problem going back to R1C1 formatting in that code.

Cheers. Hopefully anyone else who finds this problem will have this thread come up on their search results and they can benefit as well.
 
Upvote 0
Do you mean you converted to using Range from Cells for the range to apply the formatting to or the formula?
 
Upvote 0
This is a known Excel bug, see http://support.microsoft.com/kb/895562. The solution is to use absolute references:

Code:
Sub Macro1()
With Range("A1")
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B$3=1"
    .FormatConditions(1).Interior.ColorIndex = 3
End With
End Sub

If you wish to apply the CF to a range of cells, you need to set up a loop:

Code:
Sub Macro1()
Dim i as Integer
For I = 1 to 10
    With Range("A" & i)
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=$B$" & i & "=1"
        .FormatConditions(1).Interior.ColorIndex = 3
    End With
End Sub
Next i
 
Last edited:
Upvote 0
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top