簡體   English   中英

如何判斷excel單元格是否使用VBA應用了條件格式

[英]how to tell if an excel cell has conditional formatting applied using VBA

我有一系列應用了條件格式的單元格。

目的是在視覺上將值分別為正,負和無變化。

如何使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM