Using VBA to apply conditional formatting to a range of cells

眉间皱痕 提交于 2019-12-01 00:03:13

问题


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.

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.


回答1:


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.




回答2:


The "applies to" is inherent in the Selection that the With block is performed on.



来源:https://stackoverflow.com/questions/21280803/using-vba-to-apply-conditional-formatting-to-a-range-of-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!