如何判断 Excel 单元格是否使用 VBA 应用了条件格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28205442/
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
how to tell if an excel cell has conditional formatting applied using VBA
提问by yoshiserry
I have a range of cells which have conditional formatting applied.
我有一系列应用了条件格式的单元格。
The aim was to visually separate the values with a positive, negative, and no change.
目的是在视觉上区分具有正值、负值和无变化的值。
How can I use VBA to check if the cells have conditional formatting applied to them (such as color of the cell due to being a negative number)?
如何使用 VBA 检查单元格是否应用了条件格式(例如由于为负数而导致的单元格颜色)?
回答by Gary's Student
See if the Countis zero or not:
查看Count是否为零:
Sub dural()
MsgBox ActiveCell.FormatConditions.Count
End Sub
回答by CodeCamper
To use VBA to check if a cell has conditional formatting use the following code
要使用 VBA 检查单元格是否具有条件格式,请使用以下代码
Dim check As Range
Dim condition As FormatCondition
Set check = ThisWorkbook.ActiveSheet.Cells.Range("A1") 'this is the cell I want to check
Set condition = check.FormatConditions(1) 'this will grab the first conditional format
condition. 'an autolist of methods and properties will pop up and
'you can see all the things you can check here
'insert the rest of your checking code here
You can use parts of this code above with a for each loop to check all the conditions within a particular range.
您可以将此代码的一部分与 for each 循环一起使用,以检查特定范围内的所有条件。
回答by Peicong Chen
you can use Range.DisplayFormat to check if the conditional formatting is applied. Put some data into column A and then use the below code example
您可以使用 Range.DisplayFormat 检查是否应用了条件格式。将一些数据放入 A 列,然后使用下面的代码示例
Private Sub TestConditionalFormat()
Range("A:A").FormatConditions.AddUniqueValues
Range("A:A").FormatConditions(1).DupeUnique = xlDuplicate
Range("A:A").FormatConditions(1).Interior.Color = 13551615
Dim rCell As Range
Dim i As Long
For Each rCell In Range(Range("A1"), Range("A1").End(xlDown))
If rCell.DisplayFormat.Interior.Color = 13551615 Then
i = i + 1
End If
Next
MsgBox i & " :Cells Highlights for Duplicates."
End Sub