Search for cells with conditional formatting - excel

I have a range of cells with conditional formatting where if the cell exceeds a certain threshold value, it will be filled with red (I believe it is .color = 255).
I'm trying to create a macro that will search for cells in that range that exceed the threshold by searching for cells with .color = 255. The macro isn't working for some reason. It is unable to see that the cells are red due to conditional formatting. It can detect cells that I simply change the fill as red manually, though.
Sub macro22()
For Each m In Range("D7:L33")
If m.Interior.Color = 255 Then
ex = "exceedance"
End If
Next
Range("p22").Value = ex
End Sub
I know it is .color = 255 because if I record a macro to change the fill color of a cell to match the color I want from conditional formatting, that is the value it comes up with.

Color function doesn't return the colour if it's applied through conditional formatting. See here for alternative approaches

In case you have only one rule for CF you should check m.FormatConditions(0).Interior.Color value instead of m.Interior.Color - as more detailed response above)
0 represents the number of CF rules but starts from 0 (at least should be referenced so).

Related

VBA Count values based on font color

I need to add values of a range when its contents are of a certain font color (e.g., black). What I have is a a table where I am conditionally formatting the color of values. For e.g., if the "Status =Carry Over" then I am coloring the row Red. (see attachment).
Now, after the conditional formatting, I want to sum all the number values under a specific column that are NOT in red color.
I have a piece of vba code to add such values, but the problem is, the conditional coloring is throwing the code off. As long as I am manually coloring the rows, the code is ignoring the red rows. If I use the conditional coloring option, then even the colored rows are taken into account.
I am calling the below UDF using this formula =ConditionalColorSum(C2:C30)
Public Function ConditionalColorSum(rnge As Range) As Double
' Total only cells with red font numbers
Application.Volatile
Dim Total As Double, cl As Range
Total = 0
For Each cl In rnge.Cells
If cl.Font.Color = vbRed Then 'Change 'vbRed' to the color you want
Total = Total + cl.Value
End If
Next
ConditionalColorSum = Total
End Function
The Conditional formatting formula looks like above:
As per THIS
Actions such as changing the conditional formatting or table style of a range can cause what is displayed in the current user interface to be inconsistent with the values in the corresponding properties of the Range object. Use the properties of the DisplayFormat object to return the values as they are displayed in the current user interface.
The only way to get the color due to conditional formatting is to use DisplayFormat
If cl.DisplayFormat.Font.Color = vbRed Then
But per THIS
Note that the DisplayFormat property does not work in user-defined functions.
So one cannot use UDF to count the colors directly from the sheet. One can use a SUB but it would be easier just to use the same criteria that the custom format uses to count:
=COUNTIF(E2:E30,"Carry Over")
or to count where it is not red:
=COUNTIF(E2:E30,"<>Carry Over")

VBA Conditional Format .rank value based on a cell - Top10

I originally needed to select top 5% of the selected items but, it couldn't roundUP the number of highlighted items. For example: If there are 25 items in the list, 5% is 1.25 and Conditional Format is only selecting 1 item. According to my report, it should round it up and select 2 items.
As I couldn't find a way to do this, I decided to manually calculate the number of items which would show how many to be highlighted (in a cell).
It would be easier if there is a way to make the conditional format round up the number of selected items.
such as:
.rank = 5 (but should round it up)
.percent = True
If it is not possible,
I would like to highlight the top X values in a column using the conditional formatting.
The value X (The rank) is calculated in a cell and will change everytime the macro is run.
I've tried the code below but, it doesn't work.
Dim i As Integer
i = Workbooks("a.xlsm").Worksheets("b").Range("A1")
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = i
.Percent = False
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
I'm getting the error on ".rank = i" line which makes me think it is not possible to assign a variable there or I might be assigning the cell wrongly.
I also tried ".rank = i.Value" which seems to be wrong as well (Compile error: Invalid qualifier)
I'm a newbie in vba and any help would be appreciated.
ps: using office 365.
Thank you
Update: The code above works as long as the "i" value is not zero.
I managed to create a simple If statement to prevent the error.
'In my report i cannot be below 0
If i > 0 Then
Insert the code above here
'If there is no data to be highlighted
Else
'Do Nothing
End If
If a Non-VBA solution can work for you, you can use this. I made a fake dataset, just a list of 25 values (numbers 1 to 25). You want to highlight 5% higher values. 5% of 25 = 1,25, but rounded up it's 2. So you want to highlight any cell which value is one of those top 2 values in this case.
I got this:
As you can see, values 25 and 24 are highlighted. The Conditional Formatting Rule (CFR) I'm using is based on this formula:
=RANK(A1;$A$1:$A$25;0)<=ROUNDUP(COUNTA($A$1:$A$25)*0,05;0)
This is how it works:
RANK(A1;$A$1:$A$25;0) will rank the value inside the list, in descendent order.
ROUNDUP(COUNTA($A$1:$A$25)*0,05;0) will count how many cells are in the list (25) and will get the 5% (multiplying by 0,05, change 0,05 to the % you need), and it will round it up (in this case, the output will be 2).
Last Step will compare the value of Step 1 with the value of step 2. If it's less or equal to 2 (in this case), it will be highlighted as you can see
Please, note that this CFR may not work properly if there are blanks values.
Also, the good thing is that changing the 0,05 part to whatever % you need, the CFR will update perfectly. You can even bind it to a cell, and change the value of the cell manually :)
Hope this works for you!
AppliesTo property gives you the range where the condition applies.
You can then get the cell count from that, multiply it with the 5% and round it up using a WorksheetFunction.
.Rank = WorksheetFunction.RoundUp(.AppliesTo.Cells.Count * 0.05, 0)
.Percent = False

Conditional color coding in Excel

I have a fairly simple question regarding Excel and VBA, but because I'm new to VBA, I have no idea how to implement it.
I have an entire column full of a color code. The cells in that column say either R, G, or Y.
I need a macro that will traverse that column, change all cells that say "R" to have a fill of red, change all cells that say "Y" to have a fill of yellow, and change all cells that say "G" to have a fill of green.
I know this can also be done by simply sorting the rows alphabetically and then changing the colors manually, but I am looking for an automatic way of doing that.
Just use conditional formatting. You can change both the text and the cell to be the same colour
Highlight the column with your R, Y, and G and apply conditional formatting. | Home tab > Conditional Formatting > Highlight Cell Rules > Equal to... | Enter your letter of choice and select the corresponding color scheme!
Please try this:
Range("A1").Interior.Color = RGB(0, 0, 0)`
For R = 0 To 255
For G=0 To 255
For B=0 to 255
Cells(1 , 1).Interior.Color = RGB(R, G, B)`
Next R
You can use another loop change Cells row and column.

Excel formula that reads a color in one cell and puts a value in another based on that color

I am trying to set up an if statement formula that will read the color of one cell and place a value in another based on that color. I have tried writing several if statements to do this but cant find one to read the color. The sheet is set up to read a date cell. I have conditional formatting set up to color my weekends. I need a value "200" to show on days that are not on weekends and "0" to show on weekends.
If you were able to do the conditional formatting, you should be able to use a similar formula to input the value. I am guessing it reads the date cell, figures if it is the weekend or not and then inputs the value.
Assuming, for example, the non-weekends are Red (vbRed) then create a UDF (VBA, Add Module and paste the following:
Public Function checkColor(cell)
If cell.Interior.Color = vbRed Then
checkColor = 200
Else
checkColor = 0
End If
End Function
To use the function:
For example to check A1 and put 0 or 200 in B1, in B1 type
=checkColor(A1)

Using Duplicate Detection to enter a 1/0 in a separate column

The conditional formatting options works well to identify duplicates. How could I use the detected duplicates to write a value to a separate column? 1 for duplicate, 0 for not a duplicate. I thought I could use a VBA function based on the cell colour. Excel however does not store the dup detected cell color in the normal cell color property.
Note: 99 is not the light red color number, it's just for reference.
Function LightRed(rng As Range) As Boolean
If rng.Interior.ColorIndex = 99 Then
LightRed = 0
Else
LightRed = 1
End If
End Function
As tigeravatar said, you can use
=--(COUNTIF(A:A,A1)>1)
to detect duplicates and then you can apply a condition formatting on Column B where if cell value = 1, change background color to red

Resources