PDA

View Full Version : [SOLVED:] Remove duplicates across columns



YasserKhalil
07-04-2017, 03:14 AM
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

mana
07-04-2017, 03:37 AM
1)copy
2)pastespecial transpose:=true
3)removeduplicates
4)copy
5)pastespecial transpose:=true

YasserKhalil
07-04-2017, 03:51 AM
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

mdmackillop
07-04-2017, 04:22 AM
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

YasserKhalil
07-04-2017, 05:15 AM
That's great and wonderful
Thank you very much for great help
Best and kind regards

YasserKhalil
07-04-2017, 05:41 AM
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?

mdmackillop
07-04-2017, 06:01 AM
Try this to reduce the loops
For j = i + 1 To 18

YasserKhalil
07-04-2017, 06:20 AM
Thanks a lot for this useful replies
This line does the trick

For j = i + 1 To 18

Best Regards

mdmackillop
07-04-2017, 06:35 AM
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

YasserKhalil
07-04-2017, 07:02 AM
You're amazing and fascinating .. Thank you very very much for these incredible solutions