+ Reply to Thread
Results 1 to 8 of 8

Macro to Copy duplicate values from a column to a separate sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-02-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    13

    Macro to Copy duplicate values from a column to a separate sheet

    Newbie here. In a sheet named 'raw data', I have this columnn (AA) with text values. I want a macro to first determine the duplicate values in this column and copy on these duplicate column values (not cut/remove) to a separate sheet 'analysis' in the workbook. I can't figure out how to do it.

    Below is an illustrative example. So I want the muliple x's in column AA to be copied to a new sheet.

    Any ideas?
    Untitled.png

  2. #2
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Macro to Copy duplicate values from a column to a separate sheet

    this gives sorted result
    Sub dupes()
    Dim a, c As Long, i As Long, k As Long
    
    With Sheets("analysis").Range("A:A")
    Sheets("raw data").Range("AA:AA").Copy .Cells(1)
        .Sort .Cells(1), 1, Header:=xlYes
        a = Range(.Cells(1), .Cells(1).End(4)(2))
        c = 1
        For i = 2 To UBound(a)
            If a(i, 1) <> a(c, 1) Then
                k = k + 1
                a(k, 1) = a(c, 1)
                c = i
            End If
        Next i
        .ClearContents
        .Resize(k) = a
    End With
    
    End Sub
    Excel 2007 et seq also has a Remove duplicates facility if you want to risk using that.

  3. #3
    Registered User
    Join Date
    07-02-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to Copy duplicate values from a column to a separate sheet

    Hi Kalak,

    Thanks for this. If I understand correctly, does this procedure copy all the unique items from the column in a sorted manner?

  4. #4
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Macro to Copy duplicate values from a column to a separate sheet

    Not quite.

    It copies all items to the analysis sheet, then sorts them, then eliminates the duplicates.

    If you want it done as you ask, can do it that way with advanced filter, which is OK if not too much data, or several other ways.

  5. #5
    Registered User
    Join Date
    07-02-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to Copy duplicate values from a column to a separate sheet

    ah ok.
    yi
    well what i wanted was a list of items that have duplicates. this is a complaints analysis that i am trying to kind of automate and wanted to have a macro that would take a list of entities from the column and list out the ones which had the highest frequency of complaints in a period of time.

  6. #6
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Macro to Copy duplicate values from a column to a separate sheet

    do you mean like this?
    Sub xx()
    Dim a As Object, b, c, rw As Long
    
    Set a = CreateObject("scripting.dictionary")
    a.comparemode = vbTextCompare
    Sheets("raw data").Activate
    rw = Cells(Rows.Count, "aa").End(3).Row
    b = Cells(1, "aa").Resize(rw)
    For c = 2 To rw
        a(b(c, 1)) = a(b(c, 1)) + 1
    Next c
    
    With Sheets("analysis")
        .Cells(2, 1).Resize(a.Count, 2) = _
            Application.Transpose(Array(a.keys, a.items))
        .Cells(1, 1) = b(1, 1)
        .Cells(1, 2) = "count"
    End With
    End Sub

  7. #7
    Registered User
    Join Date
    07-02-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to Copy duplicate values from a column to a separate sheet

    that is exactly it!! thanks so much!!

    i was thinking of tweaking this and was wondering if it is possible to show in the analysis sheet, only the ones which have a count of 5 or more in the list?

    sorry i am getting greedy but your code has been really really helpful.

  8. #8
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Macro to Copy duplicate values from a column to a separate sheet

    like this?
    Sub xxx()
    Dim a As Object, b, c, rw As Long
    
    Set a = CreateObject("scripting.dictionary")
    a.comparemode = vbTextCompare
    Sheets("raw data").Activate
    rw = Cells(Rows.Count, "aa").End(3).Row
    b = Cells(1, "aa").Resize(rw)
    For c = 2 To rw
        a(b(c, 1)) = a(b(c, 1)) + 1
    Next c
    
    For Each c In a.keys
        If a(c) < 5 Then a.Remove c
    Next c
    
    If a.Count > 0 Then
        With Sheets("analysis")
            .Cells(2, 1).Resize(a.Count, 2) = _
                Application.Transpose(Array(a.keys, a.items))
            .Cells(1, 1) = b(1, 1)
            .Cells(1, 2) = "count if>=5"
        End With
    Else
        MsgBox "None are >=5"
    End If
    
    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. Macro to copy column values from one sheet to another
    By eemrun in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-02-2013, 04:46 PM
  2. Macro to copy values from columns of one sheet & paste it to diffrnt Column Sequence
    By kmvasudha in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2013, 06:54 AM
  3. Search column a for duplicate data and copy entire row to duplicate sheet
    By crazyAMP in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2012, 08:21 PM
  4. Copy Values from one sheet to another, merge duplicate values together
    By abhiD in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-23-2012, 06:46 AM
  5. Macro that will copy values in Column J and paste values to Column B in new sheet
    By Phixtit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2010, 04:56 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