Sort duplicates in the column

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
I need vba to sort rows based on a column as follows.
There are sets of duplicates in the column. Case-insensitive. There are blank cells in the column.
vba to sort so that the duplicates are in the adjacent rows, otherwise keeping their original order.
Remaining cells retain same order as before.
Thank you in advance.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
In situations like this, it is often best to show us an example (a picture says 1000 words!).
Please post some sample data and your expected results.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Sorry about that. Here are examples of pre and post (desired results). Thank you in advance.

PRE

Book1
AB
1B1
2A2
3C3
4A4
5A5
6B6
7C7
8D8
9A9
10D10
Sheet1


POST (EXPECTED)

Book1
AB
1B1
2B6
3A2
4A4
5A5
6A9
7C3
8C7
9D8
10D10
Sheet1
 
Upvote 0
Try this:
I'm using col C as temporary helper column.
VBA Code:
Sub topi1_1()
Dim i As Long, n As Long
Dim va, vb
Dim d As Object

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)
For i = 1 To UBound(va, 1)
    If Not d.Exists(va(i, 1)) Then
        n = n + 1
        d(va(i, 1)) = n
        vb(i, 1) = n
    Else
        vb(i, 1) = d(va(i, 1))
    End If
Next

'using col C as temporary helper column
Range("C1").Resize(UBound(vb, 1), 1) = vb
Range("A:C").Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlNo
Range("C:C").ClearContents
End Sub
 
Upvote 0
Try this:
I'm using col C as temporary helper column.
VBA Code:
Sub topi1_1()
Dim i As Long, n As Long
Dim va, vb
Dim d As Object

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 1)
For i = 1 To UBound(va, 1)
    If Not d.Exists(va(i, 1)) Then
        n = n + 1
        d(va(i, 1)) = n
        vb(i, 1) = n
    Else
        vb(i, 1) = d(va(i, 1))
    End If
Next

'using col C as temporary helper column
Range("C1").Resize(UBound(vb, 1), 1) = vb
Range("A:C").Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlNo
Range("C:C").ClearContents
End Sub
@Akuini Brilliant. Thank you so much for your prompt help.
 
Upvote 0
@Akuini is it possible to extend the vba (or have a separate vba) which removes top row of all duplicates? Here is the example from above.

The results from the initial VBA is as follows:

Book1
AB
1B1
2B6
3A2
4A4
5A5
6A9
7C3
8C7
9D8
10D10
Sheet1


Desired results about additional vba or modified vba:

Book1
AB
1B6
2A4
3A5
4A9
5C7
6D10
Sheet1 (3)
 
Upvote 0
is it possible to extend the vba (or have a separate vba) which removes top row of all duplicates?
Is there any items that has no duplicate? Like this:
Book2
AB
1B1
2A2
3X3
4A4
5A5
6B6
7C7
8D8
9A9
10D10
Sheet3

If so, you want to keep it?
 
Upvote 0
Yes. There are items without duplicates. I need to keep them. TY.
 
Upvote 0
Try this:
VBA Code:
Sub topi1_2()
Dim i As Long, n As Long, h As Long, k As Long
Dim va, vb
Dim d As Object, e As Object

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
Set e = CreateObject("scripting.dictionary"): e.CompareMode = vbTextCompare
k = Range("A" & Rows.Count).End(xlUp).Row
va = Range("A1:A" & k)
ReDim vb(1 To UBound(va, 1), 1 To 1)
For i = 1 To UBound(va, 1)
    If Not d.Exists(va(i, 1)) Then
        n = n + 1
        d(va(i, 1)) = n
        vb(i, 1) = i
    Else
        vb(i, 1) = d(va(i, 1))
        e(va(i, 1)) = ""
    End If
Next
h = e.Count
For i = 1 To UBound(va, 1)
    If e.Count = 0 Then Exit For
    If e.Exists(va(i, 1)) Then
        vb(i, 1) = ""
        e.Remove va(i, 1)
    End If
Next

'using col C as temporary helper column
Range("C1").Resize(UBound(vb, 1), 1) = vb
Range("A:C").Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlNo
k = Range("C" & Rows.Count).End(xlUp).Row
Cells(k + 1, 1).Resize(h, 2).ClearContents
Range("C:C").ClearContents
End Sub
 
Upvote 0
@Akuini Thank you.

I thought the results will be as follows.

Book1
DE
46B6
47A4
48A5
49A9
50X3
51C7
52D10
Sheet1 (5)


Instead I get the following:

Book1
AB
1B6
2A4
3A5
4A9
5X3
6D10
7C7
Sheet1 (5)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top