+ Reply to Thread
Results 1 to 7 of 7

VB Code to display and count duplicates

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    VB Code to display and count duplicates

    I am looking for a way to identify duplicates across multiple columns in excel, list each dplicate in another sheet, and display how many times each duplicate field occurs. (See attached fo desired results)

    Thanks in advance.

    Met
    Attached Files Attached Files

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

    Re: VB Code to display and count duplicates

    Hi Met. Try it
    Sub ertert()
    Dim a, b(), x, i&, n&, s$
    With Sheets("Data to be compared").Range("A3").CurrentRegion
        a = .Value: ReDim b(1 To .Count, 1 To 2)
    End With
    On Error Resume Next
    With New Collection
        For Each x In a
            s = Trim$(x)
            If Len(s) Then
                If IsEmpty(.Item(s)) Then
                    i = i + 1: b(i, 1) = s: b(i, 2) = 1
                    .Add i, s
                Else
                    n = .Item(s): b(n, 2) = b(n, 2) + 1
                End If
            End If
        Next
    End With
    If i = 0 Then Exit Sub
    Sheets.Add
    [b1].Resize(i).NumberFormat = "#"" Times"""
    [a1:b1].Resize(i).Value = b()
    End Sub

  3. #3
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Re: VB Code to display and count duplicates

    Nilem:

    That works! However, is there a way to exclude non-duplicated fields? Rather the entries that have a count of one? I am attaching my actual spreadsheet with your addon attached to my macro. (the macro has been executed) As you will see, my vb knowledge lacks but it works. I also want to skip the first two rows because they are titles and it would be nice to name the new sheet something, is this possible? Thans for all your help.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Re: VB Code to display and count duplicates

    Adendum:

    I am only interested in the fields in column A for each of the sheets. (AKA the Lawson Item #) The other fields need to be ignored. I hope my poor explaination has not confused you.

    Thanks,
    Met

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

    Re: VB Code to display and count duplicates

    try this
    Sub Duplicate_Report()
    Dim a, b(), x, i&, k, s$, wsh As Worksheet
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        For Each wsh In ThisWorkbook.Worksheets
            If wsh.Name <> "Duplicate_Report" Then
                If Len(wsh.[a3]) Then
                    With wsh
                        a = .Range("A3", .Cells(Rows.Count, 1).End(xlUp).Offset(1)).Value
                    End With
    
                    For Each x In a
                        s = Trim$(x)
                        If Len(s) Then
                            If Not .Exists(s) Then .Item(s) = 1 Else .Item(s) = .Item(s) + 1
                        End If
                    Next x
                End If
            End If
        Next wsh
        ReDim b(1 To .Count, 1 To 2)
        For Each k In .keys
            If .Item(k) > 1 Then i = i + 1: b(i, 1) = k: b(i, 2) = .Item(k)
        Next
    End With
    
    If i = 0 Then MsgBox "No duplicate", 64: Exit Sub
    
    If Not Evaluate("ISREF('Duplicate_Report'!A1)") Then
        With Sheets.Add
            .Name = "Duplicate_Report"
            .[b1].Resize(i).NumberFormat = "#"" Times"""
        End With
    End If
    Sheets("Duplicate_Report").[a1:b1].Resize(i).Value = b()
    End Sub

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,646

    Re: VB Code to display and count duplicates

    Here's my take
    Sub test()
    Dim e
    With CreateObject("Scripting.Dictionary")
        For Each e In Sheets(1).Range("a2").CurrentRegion.Value
            If Not IsEmpty(e) Then .Item(e) = .Item(e) + 1
        Next
        For Each e In .keys
            If .Item(e) = 1 Then .Remove e
        Next
        Sheets(2).Cells(1).Resize(.Count, 2).Value = _
        Application.Transpose(Array(.keys, .items))
        Sheets(2).Cells(1).CurrentRegion.Columns(2) _
        .NumberFormat = "0"" Times"""
    End With
    End Sub
    Last edited by jindon; 04-16-2012 at 01:03 AM.

  7. #7
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Re: VB Code to display and count duplicates

    Works like a charm! Kudos to both Nilem and Jindon.

+ 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