+ Reply to Thread
Results 1 to 6 of 6

Highlight duplicate rows excluding column and vice versa another column

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Highlight duplicate rows excluding column and vice versa another column

    Hello everyone
    I have a great code for Mr. Karedog that highlights the duplicate rows with different colors ... It is working great and wonderful
    I need to make some little changes ..

    As for the attachment for example the whole range is D2:H11 ..
    column D is not important for me as it will not be duplicated .. the columns E:H is the important for me
    Columns F:H is no problem .. if all found from F:H are the same so it would be treated as duplicates

    The problem is for column E .. the duplicates would be for positive and negative values so (1 and -1 are considered duplicates)
    Here' the code and I hope it is clear
    Sub Karedog()
        Dim coll As New Collection, rng As Range, rngDelete As Range, arr, C As Long, I As Long, strKey As String, v1
    
        Set rng = Range("E2").CurrentRegion
        rng.Offset(1).Interior.ColorIndex = xlNone
        arr = rng.Value
    
        For I = 2 To UBound(arr, 1)
            strKey = arr(I, 1) & Chr$(2) & arr(I, 2) & Chr$(2) & arr(I, 3) & Chr$(2) & arr(I, 4)
                On Error Resume Next
                    coll.Add Key:=strKey, Item:=New Collection
                On Error GoTo 0
            coll(strKey).Add I
        Next I
    
        For Each v1 In coll
            If v1.Count > 1 Then
                C = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
                For I = 1 To v1.Count
                    rng.Rows(v1(I)).Interior.Color = C
                    If I > 1 Then
                        If rngDelete Is Nothing Then Set rngDelete = rng.Rows(v1(I)) Else Set rngDelete = Union(rngDelete, rng.Rows(v1(I)))
                    End If
                Next I
            End If
        Next v1
    End Sub
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Highlight duplicate rows excluding column and vice versa another column

    Any help in this topic please

  3. #3
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Highlight duplicate rows excluding column and vice versa another column

    maybe so
    Sub Karedog2()
    Dim rng As Range, rngDelete As Range
    Dim arr, c As Long, i As Long, strKey As String, v1, v2
    
    Set rng = Range("E2").CurrentRegion
    rng.Offset(1).Interior.ColorIndex = xlNone
    arr = rng.Value
    
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(arr, 1)
            strKey = Abs(arr(i, 2)) & Chr$(2) & Abs(arr(i, 3)) & Chr$(2) & _
                     Abs(arr(i, 4)) & Chr$(2) & Abs(arr(i, 5))
            If .exists(strKey) Then
                .Item(strKey) = .Item(strKey) & "~" & i
            Else
                .Item(strKey) = i
            End If
        Next i
    
        For Each v1 In .keys
            If InStr(.Item(v1), "~") Then
                c = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
                For Each v2 In Split(.Item(v1), "~")
                    rng.Rows(v2).Interior.Color = c
    '                If rngDelete Is Nothing Then Set rngDelete = rng.Rows(v2) Else Set rngDelete = Union(rngDelete, rng.Rows(v2))
                Next v2
            End If
        Next v1
    End With
    End Sub

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Highlight duplicate rows excluding column and vice versa another column

    Thank you very much Mr. Nilem for this great and wonderful help
    Really great

    Now what if I need to apply this part at the end of the code
        If Not rngDelete Is Nothing Then
            If MsgBox("Do You Want To Remove Duplicates ?", vbYesNo) = vbYes Then rngDelete.Delete xlShiftUp
        End If
    Now I need to display a message for the user to delete duplicates ..
    I mean to keep only (one pair of values : positive and negative) and delete the rest ..
    In our sample rows 11 only to be deleted ..Hope it is clear

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Highlight duplicate rows excluding column and vice versa another column

    not sure, but maybe so
    Sub Karedog3()
    Dim rng As Range, rngDelete As Range, k&
    Dim x, c As Long, i As Long, strKey As String, v1, v2
    
    Set rng = Range("E2").CurrentRegion
    rng.Offset(1).Interior.ColorIndex = xlNone
    x = rng.Value
    
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(x)
            strKey = Abs(x(i, 2)) & Chr$(2) & Abs(x(i, 3)) & Chr$(2) & _
                     Abs(x(i, 4)) & Chr$(2) & Abs(x(i, 5))
            If .exists(strKey) Then
                .Item(strKey) = .Item(strKey) & "~" & i
            Else
                .Item(strKey) = i
            End If
        Next i
    
        For Each v1 In .keys
            If InStr(.Item(v1), "~") Then
                c = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256)): k = 0
                For Each v2 In Split(.Item(v1), "~")
                    rng.Rows(v2).Interior.Color = c
                    k = k + 1
                    If k > 2 Then
                        If rngDelete Is Nothing Then Set rngDelete = rng.Rows(v2) Else Set rngDelete = Union(rngDelete, rng.Rows(v2))
                    End If
                Next v2
            End If
        Next v1
    End With
    If Not rngDelete Is Nothing Then If MsgBox("Do You Want To Remove Duplicates ?", vbYesNo) = vbYes _
       Then rngDelete.Delete xlShiftUp
    End Sub

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Highlight duplicate rows excluding column and vice versa another column

    Thank you very much for awesome solution
    Best and kind regards

+ 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. How to swop data in a table with the column field and vice versa?
    By damienchew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2014, 12:18 AM
  2. how do i turn column letters to numbers and vice versa
    By intothewild in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2014, 08:27 AM
  3. [SOLVED] Change a table (col to rows and vice versa)
    By masben in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-29-2013, 06:32 AM
  4. Replies: 1
    Last Post: 02-01-2013, 03:38 PM
  5. Replies: 4
    Last Post: 03-31-2010, 09:54 PM
  6. [SOLVED] I want columns of worksheet to become rows and vice-versa.
    By Swapping rows and columns in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-10-2006, 01:10 AM
  7. How do I change rows content to column & vice versa Excel spread s
    By Harinath in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-13-2005, 06:06 PM

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