如何判断excel单元格是否使用VBA应用了条件格式

时间:2022-10-09 20:25:42

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检查单元格是否应用了条件格式(例如,由于是否为负数,单元格的颜色)?

2 个解决方案

#1


2  

See if the Count is zero or not:

查看Count是否为零:

Sub dural()
    MsgBox ActiveCell.FormatConditions.Count
End Sub

#2


1  

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.

您可以使用上面代码的部分内容和每个循环来检查特定范围内的所有条件。

#1


2  

See if the Count is zero or not:

查看Count是否为零:

Sub dural()
    MsgBox ActiveCell.FormatConditions.Count
End Sub

#2


1  

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.

您可以使用上面代码的部分内容和每个循环来检查特定范围内的所有条件。