Highlight Duplicate Values In Different Colours AND ignore blanks

dslhs

New Member
Joined
Apr 4, 2022
Messages
48
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I've borrowed a VBA code from another site that allows me to highlight duplicate values in different colours:

VBA Code:
Sub ColorCompanyDuplicates()
'Updateby Extendoffice
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim xCellPre As Range
    Dim xCIndex As Long
    Dim xCol As Collection
    Dim I As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xCIndex = 2
    Set xCol = New Collection
    For Each xCell In xRg
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
        xCIndex = xCIndex + 1
        Set xCellPre = xCol(xCell.Text)
        If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
        xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
        MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
        Exit Sub
      End If
      On Error GoTo 0
    Next
End Sub



This works like a charm, but I want it to ignore blank cells (the blank cells have an array formula but no result). How do I amend this VBA to do that?

Many thanks,
 
Last edited by a moderator:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this:

Rich (BB code):
Sub ColorCompanyDuplicates()
  'Updateby Extendoffice
  Dim xRg As Range
  Dim xTxt As String
  Dim xCell As Range
  Dim xChar As String
  Dim xCellPre As Range
  Dim xCIndex As Long
  Dim xCol As Collection
  Dim I As Long
  On Error Resume Next
  If ActiveWindow.RangeSelection.Count > 1 Then
  xTxt = ActiveWindow.RangeSelection.AddressLocal
  Else
  xTxt = ActiveSheet.UsedRange.AddressLocal
  End If
  Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
  If xRg Is Nothing Then Exit Sub
  xCIndex = 2
  Set xCol = New Collection
  For Each xCell In xRg
    If xCell <> "" Then
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
      xCIndex = xCIndex + 1
      Set xCellPre = xCol(xCell.Text)
      If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
        xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
        MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
        Exit Sub
      End If
      On Error GoTo 0
    End If
  Next
End Sub
 
Upvote 0
@dslhs
When posting vba code in the forum, please use the available code tags. It makes the code much easier to read and debug. :)
My signature block below has more details.
I have added the tags for you this time.
 
Upvote 0
Hi,

Just a follow-up - is there a way of setting it so that the same colour is used for the same value across the whole workbook (multiple sheets). Because it picks at random for each sheet, when it finds Student 1 in Sheet 1 - it makes it Green. But then I run again in Sheet 2 - it makes Student 1 Blue.

Many thanks,
 
Upvote 0
Any ideas on the above? It would be very useful and I can't work it out myself
 
Upvote 0
Yes- but no response.

I'm basically looking for a VBA that-
- identifies duplicates across the whole workbook/multiple sheets
- highlights duplicate cells to same random colour (preferably a light colour so text is still visible)
- ignores blank cells

Is this possible?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top