+ Reply to Thread
Results 1 to 2 of 2

Highlighting all duplicates except the last instance

Hybrid View

bopsgtir Highlighting all duplicates... 04-16-2013, 05:22 PM
tehneXus Re: Highlighting all... 04-16-2013, 06:57 PM
  1. #1
    Forum Contributor
    Join Date
    10-31-2010
    Location
    london
    MS-Off Ver
    Excel 365
    Posts
    152

    Highlighting all duplicates except the last instance

    Hi All, I have a problem and this is beyond my ability.

    I have a sheet of data that is never sorted or filtered, and I need to only use the last instance of any duplicate, what id like to do is highlighted all previous duplicates but NOT the last instance.

    Im sure this is possible but I have no idea where to start, any help would be appriciated.

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Highlighting all duplicates except the last instance

    Hi,

    I assumed the data to look for is in column A, try this:
    Option Explicit
    
    Sub bopsgtir()
        Const hColor = 52479
        Dim coll As New Collection
        Dim xlRng As Range, s1stAddr As String, sLastAddr As String
        Dim i As Long, lCount As Long
        
        With ActiveSheet
        
            i = 2
            Do Until .Cells(i, 1).Value = vbNullString 'get uniques
                On Error Resume Next
                coll.Add CStr(.Cells(i, 1).Value), CStr(.Cells(i, 1).Value)
                On Error GoTo 0
                i = i + 1
            Loop
            
            For i = 1 To coll.Count
                Set xlRng = .Columns(1).Find(What:=coll(i), LookIn:=xlValues, lookat:=xlWhole)
                If Not xlRng Is Nothing Then
                    s1stAddr = xlRng.Address
                    lCount = 1
                    Do
                        If lCount > 1 Then
                            xlRng.Interior.Color = hColor
                        End If
                        Set xlRng = .Columns(1).FindNext(xlRng)
                        If s1stAddr <> xlRng.Address And Not xlRng Is Nothing Then
                            sLastAddr = xlRng.Address
                            lCount = lCount + 1
                        End If
                    Loop Until s1stAddr = xlRng.Address Or xlRng Is Nothing
                    If lCount > 1 Then
                        .Range(sLastAddr).Interior.Color = xlNone
                        .Range(s1stAddr).Interior.Color = hColor
                    End If
                End If
            Next i
        End With
    End Sub
    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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