Conditional Formatting in Excel VBA

1968rm

Board Regular
Joined
Dec 22, 2010
Messages
172
I am trying to delete all conditional formatting from a sheet, and add new formatting. I use the following macro (it's recorded) but when I run it, it gives me an error by .ThemeColor = xlThemeColorDark1 towards the end of the macro. The first conditional format highlights the cell yellow, and the second one changes the font to white.

Code:
Sub PLConditionalFormatting()
    Cells.FormatConditions.Delete
    Columns("L:L").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=A1=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Columns("F:L").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=B1=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the Board,

I think the problem has to do with the way Column L is overlapped in the Selection ranges for the two rules. If you flip the sequence in which you define the two rules, it will work.

Here is version that is cleaned up a bit to remove some of the unneeded parts added by the macro recorder:
Code:
Sub PLConditionalFormatting()
    Cells.FormatConditions.Delete
 
    With Columns("F:L")
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=B1=0"
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            With .Font
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = 0
            End With
            .StopIfTrue = False
        End With
    End With
 
    With Columns("L:L")
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=A1=0"
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            With .Interior
                .PatternColorIndex = xlAutomatic
                .Color = 65535
                .TintAndShade = 0
            End With
            .StopIfTrue = False
        End With
    End With
 
    ActiveCell.Font.ThemeColor = xlThemeColorDark1
End Sub

I believe the code doesn't work when the rule for Column L is defined first, because at the point the code reaches the second instance of:
Code:
With Selection.FormatConditions(1).Font
...this expression is referring to one rule for part the Selection and a different rule for the rest of the Selection. Not positive about that reason for the error, but I'm positive it works when the order is flipped. :)
 
Upvote 0
@JS411 Thanks! works perfectly!

My problem was with the font only, if I did the same as my original but put a fill in the formatting, it worked, but not with a font color. I can't figure that out. But now with your macro - a breeze!
Now my question: Why did you finish off with this?
Code:
ActiveCell.Font.ThemeColor = xlThemeColorDark1
What does it accomplish?
 
Upvote 0
Oops..that last line I left in my mistake. :oops: When initially trying to figure out the reason for the error, I ran that as a test to isolate it from the Conditional Formatting.

Glad this worked for you. I just realized in flipping the order of the two rules you would need to make sure that it applied the intended rule to Column L.

Cheers! :)
 
Upvote 0
@JS411 I just realized a weird thing. The formulas I used were "=$U1=TRUE" for the white font and "=$Y1=TRUE" for the yellow fill, but the conditional formatting changed that to =$U1048558=TRUE for the white font and =$Y1048558=TRUE for the yellow fill. Would you know why that happens and how I can correct it? It throws off my formatting to different rows... Thanks for your help!
 
Upvote 0
Figured it out. Cell A1 needs to be selected at the time the macro is run. Works like a breeze now!! :)
 
Upvote 0
Hi all,
Came upon this thread whilst trying to find out why Excel was putting in random row number in the formatting formula
I have this code that I adapted from the net, and have just added a comment about Cell A1
Code:
' Cell A1 needs to be selected for the formatting to work, else it puts in a random row number
    'Range("A1").Select
    'With Range("G3:G" & lLastRow - 2).FormatConditions.Add( _
    '    Type:=xlExpression, _
    '    Formula1:="=NOT(ISBLANK($I3))")
    '    .Interior.Color = RGB(161, 212, 144)
     '   '.Font.Color = RGB(0, 97, 0)
    'End With

Here I am trying to highlight cells in column G if corresponding cell in column I is not blank (it will have a date)
The workbook is created from scratch and data pasted, formatted and saved.
On my first attempts Excel was doing the same thing, changing $I3 to $I1048576, when the last cell is only I&lLastRow

I know which cells have been populated with data, so know the range to apply to (that seems to stay correct?), but how do I get the condiitional formula to address the correct cell?
Code is commented out for now, hence the '
TIA
 
Last edited:
Upvote 0
Thank you.
I'll give that a go. It would also help when columns are moved.
Off to find the syntax for that.

Thanks again.
 
Upvote 0
suggest set Excel to R1C1 notation (tools. options, etc) and generate the formula. copy it to VBA. reset to normal notation

after a little familiarity R1C1 is easy enough
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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