Using VBA to apply conditional formatting to a range of cells

winhung picture winhung · Jan 22, 2014 · Viewed 79k times · Source

I would like to know how to access the column in conditional formatting titled 'Applies To' and input my own conditions. I have included a screenshot for better reference.

Applies To column

My code for adding the syntax in conditional formatting is,

With Selection
  .FormatConditions.Delete
  .FormatConditions.Add Type:=xlExpression, Formula1:="=" & c.Address & "=TRUE"
  .
  .
  .
End With

I believe the code should be added in there but i just cannot find the correct syntax.

Update :

I updated my code to look like this,

With Range(Cells(c.Row, "B"), Cells(c.Row, "N"))
  .FormatConditions.Delete
  .FormatConditions.Add Type:=xlExpression, Formula1:="=" & c.Address
  .FormatConditions(1).Interior.ColorIndex = 15 'change for other color when ticked
End With

This would essentially make rows of a specific range relevant to where i placed the checkbox, have their background colour changed. The checkbox position is represented by c.Address where 'c' contains the location of the cell that i selected to place my checkbox.

Answer

Dmitry Pavliv picture Dmitry Pavliv · Jan 22, 2014

You need to do something like this (Range("A25") is exactly what you are going to find):

With Range("A25")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, _
            Formula1:="=" & c.Address 
        '.
        '.
        '.
End With

and there is no need to write "=" & c.Address & "=TRUE", you can use just "=" & c.Address.