+ Reply to Thread
Results 1 to 5 of 5

Remove duplicates from all columns.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Remove duplicates from all columns.

    Hi all

    I have been looking around for a code to remove duplicates from multiple columns however none appear to do what I need.
    Even the Remove Duplicate from the data tab does not help me.

    What I have:
    There are several columns of numbers which are separated by a full stop.
    The number of column's will not be able to be predetermined.

    What I need:
    I need a vba code that will look at the entire sheet and remove any duplicated cells and leave only one of the cells (which was duplicated).
    Then To save space on the sheet, I would also like it to re-order the sheet from smallest to largest. (this would move cells to different columns if needed)

    attached is an example
    Attached Files Attached Files

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Remove duplicates from all columns.

    Maybe :

    Private Sub QuickSort(varr1ay As Variant, inLow As Long, inHi As Long)
      Dim pivot As Variant, tmpSwap As Variant, tmpLow As Long, tmpHi  As Long
      tmpLow = inLow
      tmpHi = inHi
      pivot = varr1ay((inLow + inHi) \ 2)
      While (tmpLow <= tmpHi)
        While (varr1ay(tmpLow) < pivot And tmpLow < inHi): tmpLow = tmpLow + 1: Wend
        While (pivot < varr1ay(tmpHi) And tmpHi > inLow): tmpHi = tmpHi - 1:    Wend
        If (tmpLow <= tmpHi) Then
           tmpSwap = varr1ay(tmpLow)
           varr1ay(tmpLow) = varr1ay(tmpHi)
           varr1ay(tmpHi) = tmpSwap
           tmpLow = tmpLow + 1
           tmpHi = tmpHi - 1
        End If
      Wend
      If (inLow < tmpHi) Then QuickSort varr1ay, inLow, tmpHi
      If (tmpLow < inHi) Then QuickSort varr1ay, tmpLow, inHi
    End Sub
    Sub Test()
      Dim coll As New Collection, arr1(), arr2(), i As Long, j As Long, k As Long, v
      With Range("A1").CurrentRegion
        arr1 = .Value
        On Error Resume Next
           For j = 1 To UBound(arr1, 2)
               For i = 1 To UBound(arr1, 1)
                   coll.Add Key:=CStr(arr1(i, j)), Item:=arr1(i, j)
               Next i
           Next j
        On Error GoTo 0
        ReDim arr2(1 To coll.Count)
        k = 0
        For Each v In coll
            k = k + 1
            arr2(k) = v
        Next v
        QuickSort arr2, 1, UBound(arr2)
        ReDim arr1(1 To UBound(arr1, 1), 1 To UBound(arr1, 2))
        k = 0
        For j = 1 To UBound(arr1, 2)
            For i = 1 To UBound(arr1, 1)
                k = k + 1
                If k > UBound(arr2) Then GoTo skipper
                arr1(i, j) = arr2(k)
            Next i
        Next j
    skipper:
        .Offset(, 7).Value = arr1
      End With
    End Sub
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: Remove duplicates from all columns.

    Thanks, looks great.

    Is it possible to adjust it slightly to conform with the following situation?

    Sheet1 is continuously updated with new numbers on a loop basis (until the last row on the sheet it filled)
    Once an entire column of numbers in generated, it is moved to sheet2 where another code (hopefully your one), sorts and removes duplicates.
    After this process is complete, the data then moves to sheet3 (in the next available row), where another code is run to order the data from smallest to largest.

    I hope to be able to insert the duplication code in the middle of this entire process.

    Hope this makes sense

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Remove duplicates from all columns.

    You are welcome.

    Since you don't show your existing code, I can't help about it.

    But the idea will be like this :

    >>> Sheet1 is continuously updated with new numbers on a loop basis (until the last row on the sheet it filled)
    Your existing code should do this

    >>> Once an entire column of numbers in generated, it is moved to sheet2 where another code (hopefully your one), sorts and removes duplicates.
    Your existing code should do this (move data from Sheet1 to Sheet2)
    Then after your code has done the moving process, call my Sub.

    >>> After this process is complete, the data then moves to sheet3 (in the next available row)
    In my Sub, the output is place on range H1 around by this line of code :
    skipper:
        .Offset(, 7).Value = arr1
    Your code should locate next available cell on Sheet3 (let say its name is TargetCell), then change the code above to this :
    skipper:
        TargetCell.Resize(UBound(arr1,1),UBound(arr1,2)).Value = arr1
    >>> where another code is run to order the data from smallest to largest.
    Your existing code should do this.

  5. #5
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: Remove duplicates from all columns.

    Hi again

    Is it possible to change the following code, so it will paste the result by using all of a column before putting the results in the next available Col or cell?

    Private Sub QuickSort(varr1ay As Variant, inLow As Long, inHi As Long)
      Dim pivot As Variant, tmpSwap As Variant, tmpLow As Long, tmpHi  As Long
      tmpLow = inLow
      tmpHi = inHi
      pivot = varr1ay((inLow + inHi) \ 2)
      While (tmpLow <= tmpHi)
        While (varr1ay(tmpLow) < pivot And tmpLow < inHi): tmpLow = tmpLow + 1: Wend
        While (pivot < varr1ay(tmpHi) And tmpHi > inLow): tmpHi = tmpHi - 1:    Wend
        If (tmpLow <= tmpHi) Then
           tmpSwap = varr1ay(tmpLow)
           varr1ay(tmpLow) = varr1ay(tmpHi)
           varr1ay(tmpHi) = tmpSwap
           tmpLow = tmpLow + 1
           tmpHi = tmpHi - 1
        End If
      Wend
      If (inLow < tmpHi) Then QuickSort varr1ay, inLow, tmpHi
      If (tmpLow < inHi) Then QuickSort varr1ay, tmpLow, inHi
    End Sub
    Sub Test()
      Dim coll As New Collection, arr1(), arr2(), i As Long, j As Long, k As Long, v
      With Range("A1").CurrentRegion
        arr1 = .Value
        On Error Resume Next
           For j = 1 To UBound(arr1, 2)
               For i = 1 To UBound(arr1, 1)
                   coll.Add Key:=CStr(arr1(i, j)), Item:=arr1(i, j)
               Next i
           Next j
        On Error GoTo 0
        ReDim arr2(1 To coll.Count)
        k = 0
        For Each v In coll
            k = k + 1
            arr2(k) = v
        Next v
        QuickSort arr2, 1, UBound(arr2)
        ReDim arr1(1 To UBound(arr1, 1), 1 To UBound(arr1, 2))
        k = 0
        For j = 1 To UBound(arr1, 2)
            For i = 1 To UBound(arr1, 1)
                k = k + 1
                If k > UBound(arr2) Then GoTo skipper
                arr1(i, j) = arr2(k)
            Next i
        Next j
    skipper:
        .Offset(, 7).Value = arr1
      End With
    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. Remove duplicates across multiple columns
    By moishier in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2016, 12:17 PM
  2. Dynamacially remove duplicates from two columns into a third
    By spoursy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-11-2014, 10:58 AM
  3. Remove Duplicates in columns
    By naveenmarapaka in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-31-2014, 11:10 AM
  4. Compare 2 columns and remove duplicates in both columns when found
    By 1dtms in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-11-2013, 02:19 AM
  5. Replies: 1
    Last Post: 04-17-2008, 03:15 PM
  6. how do i compare two columns and remove duplicates?
    By aljernon805 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 12-09-2005, 12:10 PM
  7. compare two columns and remove duplicates
    By Moni39 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-05-2005, 02: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