使用 VBA 将条件格式应用于一系列单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21280803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using VBA to apply conditional formatting to a range of cells
提问by winhung
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.
这基本上会使特定范围的行与我放置复选框的位置相关,并更改它们的背景颜色。复选框位置由 c.Address 表示,其中“c”包含我选择放置复选框的单元格的位置。
采纳答案by Dmitry Pavliv
You need to do something like this (Range("A25")
is exactly what you are going to find):
你需要做这样的事情(Range("A25")
正是你要找到的):
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
.
而且不用写"=" & c.Address & "=TRUE"
,直接用就行"=" & c.Address
。
回答by teylyn
The "applies to" is inherent in the Selection that the With block is performed on.
“适用于”是执行 With 块的 Selection 中固有的。