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

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

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

Finding previous occurence

I need help in this situation:
Some hundreds of rows of column A is filled in random order with color names (white, blue, green, yellow, red). I need a formula in column B that shows the row number of the previous occurrence of that color in column A.
Example:
A B
white 0 or not found
yellow 0 or not found
yellow 2
green 0 or not found
white 1
yellow 3 (note: not `2`, which is the first occurrence, `3` is the last)
Please take a look at this formula. It will work but it requires your data to begin in row 2:
Place this formula in cell B2:
=IFERROR(LOOKUP(2,1/(A$1:A1=A2),ROW($1:1)),0)
It is NOT an array formula, so just confirm it normally, with the ENTER key.
Now copy B2 down as far as you need.
You could use a VBA function. If you haven't done VBA, you might just have to look up how to create a new module.
In a new module, paste this code:
Public Function FindColorPosition(m_Range As Range) As String
FindColor = "not found"
If m_Range.Row = 1 Then
Exit Function
End If
Dim i As Integer
For i = m_Range.Row - 1 To 1 Step -1
If m_Range.Value = Range("A" & i).Value Then
FindColor = Str(i)
Exit Function
End If
Next
End Function
Then, in cell B1 type =FindColorPosition(A1)
Then, in cell B2 type =FindColorPosition(A2)
and so on...
You will either get not found or the row in which that color was last seen.

Background formatting pairs of rows in a spreadsheet

I was needing a way to format alternating pairs of rows in a worksheet with the same background color. When looking around I found the below link:
Excel VBA: Alternate Row Colors in Range
My problem is similar except that instead of wanting every other row auto filled with a background color I need adjacent pairs of rows colored. For instance, from my start point at row 4 columns A:T would be filled, row 5 columns A:T would have the same background color, rows 6 and 7 columns A:T would be without background color, rows 8 and 9 would share the background color as rows 4 and 5, repeated until the end of the spreadsheet.
I've made an attempt at using conditional formatting for this purpose but 1) I had not been able to get the background alternating for every pair of rows from my start point and 2)it overrides the few special cases that have a different background color 3) conditional formatting does not allow me to manually format any rows that the conditional formatting function formatted.
Many thanks to the commenters for their suggestions (which put me on the right track) but due to the limitations of Conditional Formatting I cobbled together the following Macro that allows the background to be formatted to my needs without eliminating the ability to correct for special cases. The code is heavily commented to help other newbies to understand what the code means and what to modify to change the behavior of the Macro.
Sub AlternateRowColors()
''PURPOSE: To format the background color of alternating pairs of rows in a designated range of cells with values.
'' A correction to account for a possible empty row at the end of the range not having a value failing to follow the
'' desired pattern is included.
'' Example: Column A
'' Row 1: Green
'' Row 2: Green
'' Row 3: No Background Color
'' Row 3: No Background Color
'' Repeating this pattern until the end of the used cells of a worksheet.
Dim lastUsedRow As Long ''Variable to hold the last row number.
lastUsedRow = Range("A200").End(xlUp).Row ''This checks backwards from cell A200 to A1 for the last row with an entry in Column A
''and saves the row number in variable lastUsedRow. Modify this as needed for each worksheet.
If lastUsedRow Mod 2 = 0 Then ''This block of code corrects for the second row of an entry not being highlighted at the
lastUsedRow = lastUsedRow + 1 ''end of the worksheet if the first cell in the following row is blank.
Else
End If
For Each cell In Range("A4:T" & lastUsedRow) ''Sets the range of rows and columns that the background color formatting is to affect.
If cell.Row Mod 4 = 0 Then ''Highlight row if the row number has a divided remainder of zero
cell.Interior.ColorIndex = 35 ''Sets background color by using .ColorIndex instead of RGB. Change to suit your need.
Else
If cell.Row Mod 4 = 1 Then ''Highlight row if the row number has a divided remainder of 1
cell.Interior.ColorIndex = 35 ''Sets background color by using .ColorIndex instead of RGB. Change to suit your need.
End If
End If
Next cell
End Sub
''ADDITIONAL NOTES:
''NONE
Try these formulas in conditional formatting > using a formula to determine which cell's to format:
=AND(ROW()>3,MOD(ROW(),4)=1)
and
=MOD(ROW(),4)=0
both would apply to $A:$T
Put your rules for specially formatted cells after these general rules.
Hope this helps.
In conditional formatting use the formula
=Mod(Row(),4) < 2
in the cells you want the rules to apply to

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)

Search for cells with conditional formatting

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

Resources