+ Reply to Thread
Results 1 to 3 of 3

Copy Unique Distinct Values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Copy Unique Distinct Values

    Hi,

    I have a worksheet (ItemsWithDupes) of over 3900 items which includes hundreds of duplicates. I want to copy unique distinct items to another worksheet (ItemsNoDupes). Here is the code I have but it does not seem to work.

    Note: the starting locations (C8 on the source worksheet and A8 on the result worksheet) are the same as on the whole workbook.

    Private Sub Worksheet_Activate()
    
        Dim a, i As Long
        Dim dic As Object
        Dim n As Long
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        With Sheets("ItemsWithDupes")
            a = .Range("c8", .Range("c" & Rows.Count).End(xlUp)).Value
        End With
        For i = 1 To UBound(a, 1)
            If Not dic.exists(a(i, 1)) Then
                n = n + 1
                a(n, 1) = a(i, 1)
                dic(a(i, 1)) = Empty
            End If
        Next
    
        With Me
            'Makes n the larger of the number of cells in column A or the size of the dictionary
            n = WorksheetFunction.Max(.Cells(UBound(a, 1) - 7, "A"), .Cells(.Rows.Count, "A").End(xlUp).Row)
            
            With .Range(.Cells(8, "A"), .Cells(n, "A"))
                'Clears contents of column A from row 8 to row n
                .ClearContents
                'Resets n to be the size of the dictionary
                n = UBound(a, 1) - 7
                .Value = a
                .Sort Key1:=.Cells(1), Order1:=1, Header:=xlYes
            End With
        End With
    
    End Sub
    Would you kindly check my code for what I have done wrong?

    Thanks!

    UniqueItems.xlsm

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

    Re: Copy Unique Distinct Values

    1) Delete
           'Makes n the larger of the number of cells in column A or the size of the dictionary
            n = WorksheetFunction.Max(.Cells(UBound(a, 1) - 7, "A"), .Cells(.Rows.Count, "A").End(xlUp).Row)
    2) change
    With .Range(.Cells(8, "A"), .Cells(n, "A"))
    to
    With .Range("a8").resize(n)

  3. #3
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Re: Copy Unique Distinct Values

    Thanks, jindon; that works perfectly!

+ 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. Replies: 4
    Last Post: 09-08-2013, 07:32 PM
  2. Replies: 2
    Last Post: 01-21-2013, 12:07 AM
  3. Replies: 5
    Last Post: 03-13-2012, 06:05 AM
  4. Count of distinct (unique) values by day
    By velorian in forum Excel General
    Replies: 7
    Last Post: 12-06-2011, 05:03 PM
  5. Replies: 1
    Last Post: 03-02-2011, 04:14 PM

Tags for this Thread

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