Hi MickG,
Thanks so much for the explanation. As for the Dictionary, Wow, that's really complicated.
So I tested the color worksheet tabs macro and ran into these glitches when entering duplicate values into
B7:B106 (Oh I changed the range in my worksheets from B1:B100 to B7:B106).
1. The active sheet tab wasn't colored despite having a duplicate entry.
2. When the duplicates were individually cleared, the respective sheet tabs still remain colored.
3. I have an existing "Clear" macro that clears data from B7:C106 plus some other
non-congruent cells in the active sheet. When the macro was activated, the tab remained colored.
So I meddled with your original code by adding these 4 lines marked with <<< shown below.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim Ws As Worksheet, Dn As Range
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Ws In Worksheets
Ws.Tab.ColorIndex = xlColorIndexNone <<<
Ws.Range("B7:B106").Interior.ColorIndex = xlNone
Application.ScreenUpdating = True
For Each Dn In Ws.Range("B7:B106")
If Dn.Value <> "" Then
If Not .exists(Dn.Value) Then
.Add Dn.Value, Dn
Ws.Tab.ColorIndex = xlColorIndexNone <<<
Else
.Item(Dn.Value).Interior.Color = vbRed
Dn.Interior.Color = vbRed
ActiveSheet.Tab.Color = 225 <<<
Ws.Tab.Color = 225 <<<
Application.ScreenUpdating = True <<<
End If
End If
Next Dn
Next Ws
End With
End Sub
I'm certainly unsure of what I'm doing but it somehow seems to get around the said glitches. Also, I don't know if that will mess up other things.
There's another thing I need to ask you. Even when I was using your first vba code to highlight duplicates, I sometimes run into this bug : "Runtime error '1004' Unable to set the ColorIndex property of the Interior class"
and this line gets highlighted - Ws.Range("B7:B106").Interior.ColorIndex = xlNone
In every worksheet, there's an existing Worksheet Change by Val macro that stamps the date and time in cell S4 when a (WorkOrder) number is entered in cell G2. I had cell S4 locked so that the date stamp won't be accidentally erased. But in order for the macro to write in cell S4 I had the lines, ActiveSheet.Unprotect "Password" and ActiveSheet Protect "Password" before and after the Now() function.
I later found that it was the ActiveSheet.Protect that triggered the Runtime error '1004'. As long as I have this line in any Sub, be it the "Clear" or the "Sort" macro, it triggers the error. With the ActiveSheet.Protect line remarked (') it runs ok.
What I cannot understand is why the error occurs when "B7:B106" is not locked, whether or not the worksheet is protected. And I need to use the ActiveSheet.UnProtect and Protect lines to have cell S4
locked so that the user does not accidentally erase the date and time.
Is there any way to get around this? Would it be ok to send you my workbook via email?
My apologies for the longwinded message. Hope it's not confusing.
Thank you
khtan