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

11

我想知道如何访问条件格式中标题为“应用于”的列,并输入自己的条件。为了更好地参考,我已经包含了一张屏幕截图。

应用于列

我添加条件格式语法的代码如下:

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

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

更新:

我将我的代码更新为以下内容:

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

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


2
谢谢,你的提示帮了我很多。 - winhung
2个回答

10
你需要像这样做(Range("A25") 就是你要找的内容):
With Range("A25")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, _
            Formula1:="=" & c.Address 
        '.
        '.
        '.
End With

不需要写成 "=" & c.Address & "=TRUE"的形式,只需要写成"=" & c.Address即可。


像IsBlank这样需要参数的函数,它该怎么运作? - Sergei Wallace

5
"With"块所执行的选择中自带“适用范围”。

1
嗨teylyn,您所说的“inherent”是什么意思?您是否指有一种语法可以满足选择中的“应用于”? - winhung
1
我的意思是你选择了一系列的单元格。这个范围将被用于“应用于”。 - teylyn
1
哦,好的,我明白了。是的,那很有道理。 - winhung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接