Help With Sorting

Bwachtel

New Member
Joined
Jul 31, 2015
Messages
22
I am trying to sort a column of data that has many duplicates and blanks. I would like to do this with a formula and try to make it dynamic. For the end result, i would like to have a column with the original data that is sorted 1-Z and has all duplicates removed.

Any help is appreciated!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I can't do it with a formula but I can do it with a Macro.


Open the VBA Editor (If you don't know how to do that, Hold Alt and Press F11)
In the VBA Project on the left hand side, righ-click in a space, then click Insert -> Module, then in the VBA window that pops up, paste the below code and then close the VBA module.


When in your spreadsheet and you want to sort your data, Hold ALT and press F8, then click the macro SORT and click on RUN.


Hope this helps.




Code:
Sub Sort()
'
' Sort Macro
'


'Creates a start point to come back to later as I don't know the name of your worksheet
'Replace "A1" and "A:A" below with the cell reference of the Label at the top of the column that needs sorting


    Dim rng As Range
    Set rng = Range("A1")
    rng.Name = "Start_Here"


'Filters the data to remove duplicates
    Columns("A:A").Select
    Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    Selection.Copy
    
'Adds a new sheet to put the data into
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Temp"
    End With


'Pastes the data
    ActiveSheet.Paste
    Application.CutCopyMode = False


'Sorts the data
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range( _
        "A2:A1048568"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A1:A1048568")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Application.Goto Reference:="Start_Here"
    Columns("A:A").Select
    ActiveSheet.ShowAllData
    Selection.ClearContents
    Application.Goto Reference:="Temp!R1C1"


'Remember to replace "A:A" below with the relevant column
    Columns("A:A").Select
    Selection.Copy
    
'Pastes the data back where it came from
    Application.Goto Reference:="Start_Here"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range( _
        "A2:A1048568"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A1048568")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


'Deletes the "Temp" sheet
    Application.DisplayAlerts = False
    Sheets("Temp").Select
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
    Application.Goto Reference:="Start_Here"
End Sub
 
Last edited:
Upvote 0
Maybe the attached link will help for a formula way of doing this.

Excel Magic Trick 759: Array Formula To Sort List & Remove Duplicates - Dynamic Named Range
https://www.youtube.com/watch?v=IZLAzIYfMDU


I tried but didnt seem to work. Some values were missing. I did some more searching and found another post with the same question. I replied to that thread and tried a modified version of the formula from that posted video. I left a detailed reply there with my code and table. the link to the thread is below if maybe you could take a look at it?

Once again all help is greatly appreciated!

https://www.mrexcel.com/forum/excel...tes-list-sort-alphabetically.html#post4973711
 
Upvote 0
I appreciate the help, but unfortunately i cannot use macros for my solution. Worst case, i will just manually type it, but would prefer to have something a little more dynamic. Thanks Again!!

I can't do it with a formula but I can do it with a Macro.


Open the VBA Editor (If you don't know how to do that, Hold Alt and Press F11)
In the VBA Project on the left hand side, righ-click in a space, then click Insert -> Module, then in the VBA window that pops up, paste the below code and then close the VBA module.


When in your spreadsheet and you want to sort your data, Hold ALT and press F8, then click the macro SORT and click on RUN.


Hope this helps.




Code:
Sub Sort()
'
' Sort Macro
'


'Creates a start point to come back to later as I don't know the name of your worksheet
'Replace "A1" and "A:A" below with the cell reference of the Label at the top of the column that needs sorting


    Dim rng As Range
    Set rng = Range("A1")
    rng.Name = "Start_Here"


'Filters the data to remove duplicates
    Columns("A:A").Select
    Range("A:A").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    Selection.Copy
    
'Adds a new sheet to put the data into
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Temp"
    End With


'Pastes the data
    ActiveSheet.Paste
    Application.CutCopyMode = False


'Sorts the data
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range( _
        "A2:A1048568"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A1:A1048568")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Application.Goto Reference:="Start_Here"
    Columns("A:A").Select
    ActiveSheet.ShowAllData
    Selection.ClearContents
    Application.Goto Reference:="Temp!R1C1"


'Remember to replace "A:A" below with the relevant column
    Columns("A:A").Select
    Selection.Copy
    
'Pastes the data back where it came from
    Application.Goto Reference:="Start_Here"
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range( _
        "A2:A1048568"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A1048568")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


'Deletes the "Temp" sheet
    Application.DisplayAlerts = False
    Sheets("Temp").Select
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
    Application.Goto Reference:="Start_Here"
End Sub
 
Upvote 0
Try this:

Excel Workbook
ABC
2JohnJohnAnn
3PeterPeterDon
4AnnAnnJames
5JamesJamesJohn
6PeterPeterMary
7JamesJamesPeter
8zzzThomas
9TomTomTom
10ThomasThomas
11DonDon
12JohnJohn
13MaryMary
14zzz
15AnnAnn
16
Sheet2
 
Upvote 0
What i am trying to sort is below. There are no blanks to worry about since i am using a dynamic range. Its the combination of numbers and letters that is throwing me off such as 35A and 66B.

HavePreferCould work too
99135A
35A566B
66B201
135A5
2066B20
59999

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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