Sep 29 2024 07:14 AM
Hello All Excelers,
Oh Boy why is this not working???
The code below is supposed to identify cells that are duplicates in a range of cells.
I apply the Conditional Formatting (CFs) to let Excel find the duplicates and then I test to see if the Font Color and Interior color are the ones assigned by the CFs and if they are then let me know.
rngCellsToCombine is a range of cells that need to be combines into another range to go to a MS-Access Database table. rngCellsToCombine has a header so I resize it to eliminate the header row.
Then I apply the CFs to color the duplicates.
Then I loop cell by cell to find the duplicates based on the Font and Interior colors.
BUT THIS DOES NOT WORK! The message is always coming up regardless if the cell is a duplicate or not!!! WHY??? Can you see something I am missing?
==============================
Sub NotWorking()
With rngCellsToCombine.Offset(-1, 0).Resize(rngCellsToCombine.Rows.Count + 1, 1)
'Apply CFs.
.FormatConditions.AddUniqueValues
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).DupeUnique = xlDuplicate
With .FormatConditions(1).Font
.Color = -16777024
.TintAndShade = 0
End With
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 10284031
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'Check if Dups are there
For Each rngCell In .Cells
Stop
If rngCell.FormatConditions(1).Interior.Color = 10284031 And rngCell.FormatConditions(1).Font.Color = -16777024 Then
rngCell.Select
MsgBox "Dups are still there"
Stop
End If
Next rngCell
End With
End Sub
==============================
Sep 29 2024 06:59 PM
@GeorgieAnne In your For Each loop, try replacing FormatConditions(1) with DisplayFormat in two places. Also, color -16777024 is equivalent to 192, so you may need to use 192 when reading back the font color:
If rngCell.DisplayFormat.Interior.Color = 10284031 And rngCell.DisplayFormat.Font.Color = 192 Then...
On a side note, it's not very clear how you've defined rngCellsToCombine, and the Offset.Resize method is doing the opposite of what you've described (it's adding a header instead of removing one). Also, if you run this code over and over again, it will create a new Conditional Formatting rule each time, so you'll end up with multiple duplicate CF rules. You may want include a bit of code at the start to check for and delete existing rules first.
Sep 30 2024 09:45 AM