我有一系列应用了条件格式的单元格。
目的是在视觉上将值分别为正,负和无变化。
如何使用VBA检查单元格是否应用了条件格式(例如,由于是否为负数,单元格的颜色)?
查看Count是否为零:
Sub dural()
MsgBox ActiveCell.FormatConditions.Count
End Sub
要使用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
您可以将上述代码的一部分用于每个循环,以检查特定范围内的所有条件。
您可以使用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