VBA COnditional Formatting Not Working

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hoping someone is able to correct me ...
With a Google search, I found some direction on how to apply conditional formatting to cells with VBA. With some adaptation, I came up with the following code:

VBA Code:
          With sh
                statval = .Range("O4")
                Set rngcf = .Range("H13:Q" & rwpdaed)
                .Unprotect
                For Each cell In rngcf
                    cell.FormatConditions.Delete
                    Debug.Print cell.Address
                    Set condition1 = cell.FormatConditions.Add(xlCellValue, xlEqual, "=" & Left(statval, 3))
                    Set condition2 = cell.FormatConditions.Add(xlCellValue, xlNotEqual, "=" & Left(statval, 3))
                    With condition1
                        .Font.Color = vbWhite
                    End With
                    With condition2
                        '.Font.colour = RGB(166, 166, 166)
                        .Interior.Color = RGB(166, 166, 166)
                    End With
                Next cell
           End With

The idea is to check each cell in the range (rngcf). If the value in that cell equals the value represented by the first three letters of 'statval', the cell's font colour turns to white. If the cell value isn't equal to the first three letters of the value 'statval', the cell background is shaded grey.

This isn't working. I'm not sure if my conditions are wrong, of the format application is wrong.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You don't need to loop through the cells. You can set the format condition for the entire range.
VBA Code:
    With sh
        Set rngcf = .Range("H13:Q" & rwpdaed)
        With rngcf.FormatConditions
            .Delete
            Set condition1 = .Add(Type:=xlExpression, Formula1:="=Left(H13,3)=$O$4")
            condition1.Font.Color = vbWhite
            Set condition2 = .Add(Type:=xlExpression, Formula1:="=Left(H13,3)<>$O$4")
            condition2.Interior.Color = RGB(166, 166, 166)
        End With
    End With
 
Upvote 1
Solution
Thank you so much. This is a much better solution than what I was dealing with.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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