【问题标题】:Using VBA to apply conditional formatting to a range of cells使用 VBA 将条件格式应用于一系列单元格
【发布时间】:2014-02-12 09:15:03
【问题描述】:

我想知道如何以条件格式访问标题为“适用于”的列并输入我自己的条件。 为了更好地参考,我附上了截图。

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

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”包含我选择放置复选框的单元格的位置。

【问题讨论】:

  • 谢谢,你的提示对我帮助很大

标签: vba excel conditional-formatting


【解决方案1】:

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

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

而且不用写"=" & c.Address & "=TRUE",直接用"=" & c.Address即可。

【讨论】:

  • 这对于像 IsBlank 这样需要参数的函数如何工作?
【解决方案2】:

“适用于”是执行 With 块的选择所固有的。

【讨论】:

  • 嗨 teylyn,你说的固有是什么意思?您的意思是有一种语法可以满足 Selection 中的“适用于”吗?
  • 我的意思是您选择了一系列单元格。这就是将用于“适用于”的范围。
  • 哦,好的,我明白了。是的,这是有道理的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多