+ Reply to Thread
Results 1 to 2 of 2

Highlighting Duplicate Cells in a Split Range

Hybrid View

oleander Highlighting Duplicate Cells... 10-08-2018, 06:34 PM
mjr veverka Re: Highlighting Duplicate... 10-09-2018, 08:21 AM
  1. #1
    Registered User
    Join Date
    07-13-2016
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    13

    Highlighting Duplicate Cells in a Split Range

    Hi everyone! I'm using the following macro to highlight cells in a range that are duplicates, but it seems to have a problem when I highlight split ranges (something like $A$1,$A$3:$A$14). Is there a way to fix this?

    Sub HighlightDuplicateValues()
    Dim myRange As Range
    Dim myCell As Range
    Set myRange = Selection
    For Each myCell In myRange
    If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
    myCell.Interior.ColorIndex = 36
    End If
    Next myCell
    End Sub
    Thanks!
    Last edited by jeffreybrown; 10-08-2018 at 06:49 PM. Reason: Please use code tags!

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,253

    Re: Highlighting Duplicate Cells in a Split Range

    Quote Originally Posted by oleander View Post
    ... a problem when I highlight split ranges (something like $A$1,$A$3:$A$14) ...
    For separated ranges, use e.g. the "Areas" property. Besides, not every function can be used in this case, you need to look for replacements, e.g.:
    Option Explicit
    
    Sub HighlightDuplicateValues_v1()
        Dim elmnt
        Dim clr As Integer: clr = 2
        Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
        Dim myRange As Range: Set myRange = Selection
        Dim sngArea As Range, myCell As Range
        
        For Each sngArea In myRange.Areas
            For Each myCell In sngArea
                If Not dict.Exists(myCell.Value) Then dict(myCell.Value) = 1 Else dict(myCell.Value) = dict(myCell.Value) + 1
            Next
        Next
        
        For Each elmnt In dict.Keys
            If dict(elmnt) > 1 Then
                clr = clr + 1
                With Application.ReplaceFormat.Interior
                    Select Case clr
                        Case Is <= 56: .ColorIndex = clr: .Pattern = xlSolid
                        Case 57 To 110: .ColorIndex = clr - 54: .Pattern = xlGray8: .PatternColorIndex = xlAutomatic
                        Case Else: .ColorIndex = 0: .Pattern = xlCrissCross: .PatternColorIndex = xlAutomatic
                    End Select
                End With
                myRange.Replace What:=elmnt, Replacement:=elmnt, LookAt:=xlWhole, SearchOrder:=xlByRows, ReplaceFormat:=True
            End If
        Next
        
        Set myRange = Nothing
        dict.RemoveAll: Set dict = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Highlighting Duplicate Data Using Two Cells
    By mowens74 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2018, 10:21 AM
  2. Highlighting Duplicate cells?
    By Rexx372 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-20-2017, 07:09 PM
  3. Highlighting Duplicate Cells
    By vinceancona in forum Excel General
    Replies: 5
    Last Post: 09-28-2013, 02:22 PM
  4. [SOLVED] Highlighting Duplicate Cells
    By paodelol in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 02-11-2013, 11:15 AM
  5. Finding duplicate cells and highlighting them
    By masond3 in forum Excel General
    Replies: 5
    Last Post: 08-07-2012, 08:35 AM
  6. highlighting duplicate cells
    By langdon37 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-05-2009, 05:58 AM
  7. How to Highlighting duplicate ALL cells on A:A column with red ?
    By yannipr in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-19-2008, 04:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1