使用VBA将条件格式应用于一系列单元格

win*_*ung 9 excel vba conditional-formatting excel-vba

我想知道如何以标题为"适用于"的条件格式访问该列并输入我自己的条件.我已经包含了截图以供更好的参考.

适用于列

我在条件格式中添加语法的代码是,

With Selection
  .FormatConditions.Delete
  .FormatConditions.Add Type:=xlExpression, Formula1:="=" & c.Address & "=TRUE"
  .
  .
  .
End With
Run Code Online (Sandbox Code Playgroud)

我相信应该在那里添加代码,但我找不到正确的语法.

更新:

我更新了我的代码,看起来像这样,

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
Run Code Online (Sandbox Code Playgroud)

这基本上会使特定范围的行与我放置复选框的位置相关,其背景颜色会发生变化.复选框位置由c.Address表示,其中'c'包含我选中放置复选框的单元格的位置.

Dmi*_*liv 8

你需要做这样的事情(Range("A25")正是你要找到的):

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

而且没有必要写"=" & c.Address & "=TRUE",你可以使用"=" & c.Address.


tey*_*lyn 5

"适用于"是执行With块的选择中固有的.