• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to remove duplicates and sort the data in a specific way

koskesh

Member
Hi, I need help from you guys.

Is it possible to create a Macro that does following:

- Finds the Column where the Data is
- Paste the values in the column next to it in following way: A; B; C; (duplicates already removed)

Attached a file for better explanation.

Thanks
 

Attachments

  • output.xlsx
    8.1 KB · Views: 17
Hi,

I haven't used sorting yet so no idea about it. however following code will join them.

Code:
Function JoinUnique(myrng As Range) As String
Dim r As Range
Application.ScreenUpdating = False

With CreateObject("scripting.dictionary")
    For Each r In myrng
        If Len(r) > 0 And Not .Exists(r.Value) Then .Add r.Value, 1
    Next
JoinUnique = Join(.Keys, ",")
End With

Application.ScreenUpdating = True
End Function
 
One thing just clicked!!

Something like this. You are free to manipulate it.

Code:
Option Explicit

Sub Unique()
Dim r As Range, col As String
Range("A1:A18").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("G1"), Unique:=True
Range("G2:G4").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("G2"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
For Each r In Selection
    col = col & "," & r
Next
[B2] = Right(col, Len(col) - 1)
[G1].CurrentRegion.Cells.Clear
[B2].Select
End Sub
 
Thank you, how do I have to change the macro if I want the macro to look in column A (not just A1:A18)
and is it possible to include empty cells in column A

For Example:

A
B
C

D
 
This one!!

Code:
Option Explicit

Sub Unique1()
Dim r As Range, col As String

With ActiveSheet
.Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.[G1], Unique:=True

.Range(.[G1], .Cells(.Rows.Count, "G").End(xlUp).Address).Sort Key1:=.[G1], Order1:=xlAscending

For Each r In .[G1].CurrentRegion
    col = col & "," & r
Next

.[B2] = Right(col, Len(col) - 1)
.[G1].CurrentRegion.Cells.Clear

End With

End Sub
 
Thanks Deepak, the first time it worked, second time I got this error:
Sort method of Range class failed

.Range(.[G1], .Cells(.Rows.Count, "G").End(xlUp).Address).Sort Key1:=.[G1], Order1:=xlAscending
 
Thanks Deepak, the first time it worked, second time I got this error:
Sort method of Range class failed

.Range(.[G1], .Cells(.Rows.Count, "G").End(xlUp).Address).Sort Key1:=.[G1], Order1:=xlAscending

I didn't encounter any issue so far!!!

You might doing something wrong there. try this.

Code:
Option Explicit

Sub Unique2()
Dim r As Range, col As String

Application.ScreenUpdating = False

With ActiveSheet
 .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.[G1], Unique:=True
 .Range("G1:G" & .Range("G" & .Rows.Count).End(xlUp).Row).Sort Key1:=.[G1], Order1:=xlAscending

For Each r In .[G1].CurrentRegion
     col = col & "," & r
Next

 .[B2] = Right(col, Len(col) - 1)
 .[G1].CurrentRegion.Cells.Clear
End With

Application.ScreenUpdating = True

End Sub
 
Back
Top