Consulting

Results 1 to 10 of 10

Thread: Remove duplicates across columns

  1. #1

    Remove duplicates across columns

    Hello everyone
    I have data in range("A1:R6") and I need to remove duplicate columns ... I have highlighted the duplicates in the attachment file
    I need to eliminate columns O:R ...
    Thanks advanced for help
    Attached Files Attached Files

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    1)copy
    2)pastespecial transpose:=true
    3)removeduplicates
    4)copy
    5)pastespecial transpose:=true

  3. #3
    Thanks a lot for reply
    I have already done the same steps but I need a code as it is part of another big code ..
    Thank you very much

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Dim i, j
    Dim Data As Range
    
    
    Set Data = Cells(1, 1).CurrentRegion
    For i = 1 To 18
    For j = 2 To 18
    If i <> j Then
    If Join(Application.Transpose(Data.Columns(i).Value), "") = _
        Join(Application.Transpose(Data.Columns(j).Value), "") _
        Then Data.Columns(j).ClearContents
    End If
    Next j
    Next i
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    That's great and wonderful
    Thank you very much for great help
    Best and kind regards

  6. #6
    Sorry for diturbing you again... After testing the code on the original file ( with a lot of columns) it took some time ..
    Is there a way to make it faster?

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try this to reduce the loops
    For j = i + 1 To 18
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Thanks a lot for this useful replies
    This line does the trick

    For j = i + 1 To 18

    Best Regards

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Alternative method, should be much quicker.
    Sub Test2()
        Dim Dic, i, j
        Dim data As Range
        Dim Col
        
        Set Dic = CreateObject("Scripting.Dictionary")
        Set data = Cells(1, 1).CurrentRegion
        Col = data.Columns.Count
        For i = 1 To Col
            Dic.Add i, Join(Application.Transpose(data.Columns(i)), "")
        Next
        For i = 1 To Col
            For j = i + 1 To Col
                If Dic(i) = Dic(j) Then
                    data.Columns(j).ClearContents
                End If
            Next
        Next
    End Sub
    Last edited by mdmackillop; 07-04-2017 at 06:37 AM. Reason: Generalised code
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    You're amazing and fascinating .. Thank you very very much for these incredible solutions

Posting Permissions

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