Results 1 to 10 of 28

Thread: Remove Duplicates From Across Multipe Columns In A Single WorkSheet

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0
    Hi,

    This is regarding 'different method':

    Tried the code, worked for about 20-30 seconds and left me with only column A populated to around 440k rows. Not just unique values cause I can't find some entries from the full 17 columns table.

    Thank you for all your help as I still can't figure this out on my own

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Quote Originally Posted by cyphrevil View Post
    Hi,

    This is regarding 'different method':

    Tried the code, worked for about 20-30 seconds and left me with only column A populated to around 440k rows. Not just unique values cause I can't find some entries from the full 17 columns table.

    Thank you for all your help as I still can't figure this out on my own
    My bad

    I tested this code with 5 columns of data.

    replace
    Code:
    For i = 1 To 5
    with
    Code:
    For i = 1 To 17
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0
    Resulted with column A with 1mln rows and B with 143k rows. Ends with values starting with 'z' so seems to be working, yet I still can't find certain values from main table.

    I'm sure you'll have some ideas.

    Thank you.
    Last edited by Admin; 10-26-2013 at 08:51 PM.

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    I modified the code slightly, but not too different. It's working on 500 rows and 17 columns. Can you try this.

    And here's the code.

    Code:
    Sub ExcelFox()
            
        Dim lng As Long
        Dim wks As Worksheet
        Dim objDic As Object
        Dim var As Variant
        Dim varIndex As Variant
        Dim lngRow As Long
        Const clngLastColumn As Long = 17
        Const clngSteps As Long = 100000
        
        Set objDic = CreateObject("Scripting.Dictionary")
        Set wks = Worksheets("NameOfSheetWithDuplicateValues")
        For lng = 1 To clngLastColumn
            With wks
                .Range(.Cells(1, lng), .Cells(.Rows.Count, lng).End(xlUp)).RemoveDuplicates Columns:=1, Header:=xlNo
                var = .Range(.Cells(1, lng), .Cells(.Rows.Count, lng).End(xlUp)).Value2
                For lngRow = 1 To UBound(var)
                    objDic.Item(var(lngRow, 1)) = 0
                Next lngRow
            End With
        Next lng
        Erase var
        var = objDic.keys
        var = Application.Transpose(Application.Transpose(var))
        Set objDic = Nothing
        If wks.Parent.FullName <> wks.Parent.Name Then
            wks.Parent.Save
        End If
        lng = 1
        For lngRow = 1 To UBound(var) + Abs(LBound(var) = 0) Step clngSteps
            varIndex = Application.Transpose(Evaluate(clngSteps * (lng - 1) & "+ ROW(1:" & Application.Min(UBound(var) + Abs(LBound(var) = 0), clngSteps) & ")"))
            Cells(1, clngLastColumn + lng).Resize(Application.Min(UBound(var) + Abs(LBound(var) = 0), clngSteps)).Value2 = Application.Transpose(Application.Index(var, varIndex))
            lng = lng + 1
        Next lngRow
        Erase varIndex
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    ...

    Quote Originally Posted by Excel Fox View Post
    I modified the code slightly, but not too different. It's working on 500 rows and 17 columns. Can you try this.

    And here's the code.

    Code:
    Sub ExcelFox()
            
        Dim lng As Long
       ...

    It's grinding it, I'll let you know as soon as it's finished.

  6. #6
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Quote Originally Posted by cyphrevil View Post
    Resulted with column A with 1mln rows and B with 143k rows. Ends with values starting with 'z' so seems to be working, yet I still can't find certain values from main table.

    I'm sure you'll have some ideas.

    Thank you.
    Again I missed the last row part. In my test I hard coded last row as 100000. Give this a try.

    Code:
    Option Explicit
    
    Sub kTest()
        
        Dim strAll  As String
        Dim i       As Long
        Dim r       As Long
        Dim n       As Long
        Dim d       As Long
        Dim fd      As String
        Dim fn      As String
        Dim objFS   As Object
        Dim objFile As Object
        Dim adoConn As Object
        Dim adoRset As Object
        
        Const Block = 65000
        
        strAll = "Temp"
        
        For i = 1 To 17
            d = 0
            n = Cells(Rows.Count, i).End(3).Row
            If n = 1 Then n = Rows.Count
            For r = 1 To n Step Block
                strAll = strAll & vbCrLf & Join(Application.Transpose(Cells(r, i).Resize(Application.Min(Block, Abs(n - d))).Value2), vbCrLf)
                d = d + Block
            Next
        Next
        
        fd = Environ("temp") & "\"
        fn = "Test.txt"
        
        Set objFS = CreateObject("scripting.filesystemobject")
        Set objFile = objFS.opentextfile(fd & fn, 2, 1)
        
        objFile.write strAll
        objFile.Close
        
        Set adoConn = CreateObject("ADODB.Connection")
        
        adoConn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
                    "Dbq=" & fd & ";Extensions=txt;"
            
        Set adoRset = CreateObject("ADODB.Recordset")
            adoRset.Open "SELECT [Temp] FROM [" & fn & "] GROUP BY [Temp]", adoConn, 3, 1, 1
                
        d = adoRset.RecordCount
        
        ActiveSheet.UsedRange.ClearContents
        
        If d > Rows.Count Then
            i = 1
            While Not adoRset.EOF
                Cells(1, i).CopyFromRecordset adoRset, 1000000
                i = i + 1
            Wend
        Else
            Range("a1").CopyFromRecordset adoRset
        End If
        
        adoRset.Close
        adoConn.Close
        
        Kill fd & fn
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Error message

    The previous one just ended with 'out of memory' message.

    I'm going to reboot the machine and try the new code. Will update asap.
    Last edited by Admin; 10-27-2013 at 10:00 AM.

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    error message

    Quote Originally Posted by cyphrevil View Post
    The previous one just ended with 'out of memory' message.

    I'm going to reboot the machine and try the new code. Will update asap.

    it's giving me error number 14 'out of string' message.

Similar Threads

  1. Find Duplicates, 2 Columns, Different Worksheets
    By ghendi in forum Excel Help
    Replies: 14
    Last Post: 07-17-2013, 04:26 AM
  2. Remove Special Characters From Text Or Remove Numbers From Text
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 5
    Last Post: 05-31-2013, 04:43 PM
  3. Print Nth Worksheet To Mth Worksheet using VBA
    By Ryan_Bernal in forum Excel Help
    Replies: 2
    Last Post: 02-28-2013, 06:57 PM
  4. Converge Data From Multiple Columns To Single Column
    By ayazgreat in forum Excel Help
    Replies: 3
    Last Post: 12-14-2012, 10:55 PM
  5. Detect Duplicates In Named Ranges With Cross Check
    By Yegarboy in forum Excel Help
    Replies: 3
    Last Post: 10-09-2012, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •