Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 440
yoshiserry Avatar asked Sep 29 '22 21:09

yoshiserry


2 Answers

See if the Count is zero or not:

Sub dural()
    MsgBox ActiveCell.FormatConditions.Count
End Sub
like image 135
Gary's Student Avatar answered Oct 05 '22 06:10

Gary's Student


To use VBA to check if a cell has conditional formatting use the following code

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.

like image 22
CodeCamper Avatar answered Oct 05 '22 06:10

CodeCamper