Need a macro to sort Rows with in location column

Alphix

New Member
Joined
Sep 29, 2016
Messages
22
Please help.
Excel is not sorting the data in the rows in location Column for some reason I do not understand why.
Need a macro to sort the Hyphened data. x-xx-xxx


Sort Macro.xlsx
DE
1LOCATIONITEM
29-6-0 bike
311-7-1 apple
43-11-3 car
511-7-3 balloon
611-7-4 bird
75-10-6 cat
89-6-29 dog
915-7-1 pear
1019-4-1 wall
1123-1-1 nails
1227-3-1 hammer
1337-3-9 screw
1439-3-1 skates
1541-7-6 ball
1641-8-5 baseball
1744-1-1 forks
1844-3-9 plates
1945-4-14 dish
2047-2-17 bucket
2147-3-1 mop
2247-3-2 can
2349-10-2 cup
2454-1-3 paper
2562-2-8 scissors
2664-1-7 mask
279-5-13.5 heater
28SC1-4-5 plant
Sheet1
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Maybe you can show how sorting result you are expecting here since it seems like Excel is sorting referring to the left most number or whatever :)
 
Upvote 0
Hi Zot, the sort result should look like below. Basically the location represents as follows: Cabinet - Drawer - location.
It should sort the left most number (Cabinet) then sort the middle number (Drawer) then right most number (location).
I think excel thinks some of the locations are dates. I am not able to do a simple sort. Thank you for your help.

Sort Macro.xlsx
M
1LOCATION
23-11-3
35-10-6
49-5-13.5
59-6-0
69-6-29
711-7-1
811-7-3
911-7-4
1015-7-1
1119-4-1
1223-1-1
1327-3-1
1437-3-9
1539-3-1
1641-7-6
1741-8-5
1844-1-1
1944-3-9
2045-4-14
2147-2-17
2247-3-1
2347-3-2
2449-10-2
2554-1-3
2662-2-8
2764-1-7
28SC1-4-5
Sheet2
 
Upvote 0
I just make use of Excel sort. First split the number/variable into 3 using -(Dash) as delimiter and create table in a sheet named Sort. There will be 4 columns. Then I just sort using column A, B and C. Rewrite that back again to original sheet.

VBA Code:
Sub SortDashNum()

Dim n&, eRow&
Dim Arry$()
Dim ws As Worksheet, wsSort As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
ActiveWorkbook.Sheets.Add.Name = "Sort"
Set wsSort = ActiveWorkbook.Sheets("Sort")

Application.ScreenUpdating = False

eRow = ws.Range("A1").End(xlDown).Row
For n = 2 To eRow
    Arry = Split(Trim(ws.Range("A" & n)), "-")
    wsSort.Range("A" & n) = Arry(0)
    wsSort.Range("B" & n) = Arry(1)
    wsSort.Range("C" & n) = Arry(2)
    wsSort.Range("D" & n) = ws.Range("B" & n)
Next

wsSort.Range("A1") = "A"
wsSort.Range("B1") = "B"
wsSort.Range("C1") = "C"
wsSort.Range("D1") = "D"

wsSort.Range("A1", "D" & eRow).Select
With wsSort
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=Range("A2", "A" & eRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("B2", "B" & eRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range("C2", "C" & eRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
With wsSort.Sort
    .SetRange Range("A1", "D" & eRow)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

For n = 2 To eRow
    With ws
        .Range("A" & n) = wsSort.Range("A" & n) & "-" & wsSort.Range("B" & n) & "-" & wsSort.Range("C" & n)
        .Range("B" & n) = wsSort.Range("D" & n)
    End With
Next
Application.DisplayAlerts = False
wsSort.Delete
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Hi Zot, this works really good.
can you please modify the macro to add additional columns?

I greatly appreciate it.

4_paste.xlsm
DEFGH
1LOCATIONITEMITEMITEMITEM
29-6-0 bikebikebikebike
311-7-1 appleappleappleapple
43-11-3 carcarcarcar
511-7-3 balloonballoonballoonballoon
611-7-4 birdbirdbirdbird
75-10-6 catcatcatcat
89-6-29 dogdogdogdog
915-7-1 pearpearpearpear
1019-4-1 wallwallwallwall
1123-1-1 nailsnailsnailsnails
1227-3-1 hammerhammerhammerhammer
1337-3-9 screwscrewscrewscrew
1439-3-1 skatesskatesskatesskates
1541-7-6 ballballballball
1641-8-5 baseballbaseballbaseballbaseball
1744-1-1 forksforksforksforks
1844-3-9 platesplatesplatesplates
1945-4-14 dishdishdishdish
2047-2-17 bucketbucketbucketbucket
2147-3-1 mopmopmopmop
2247-3-2 cancancancan
2349-10-2 cupcupcupcup
2454-1-3 paperpaperpaperpaper
2562-2-8 scissorsscissorsscissorsscissors
2664-1-7 maskmaskmaskmask
279-5-13.5 heaterheaterheaterheater
28SC1-4-5 plantplantplantplant
Sheet3
 
Upvote 0
When data in column A is split into column A, B and C, these are all the sorting column. The rest of column will just follow.

I have modified the code so that it become dynamic and able to accommodate any column number as long as Location column being mandatory in its position.

VBA Code:
Sub SortDashNum()

Dim eCol$
Dim n&, m&, k&, eRow&, colCount&
Dim Arry$()
Dim ws As Worksheet, wsSort As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
ActiveWorkbook.Sheets.Add.Name = "Sort"
Set wsSort = ActiveWorkbook.Sheets("Sort")

Application.ScreenUpdating = False

eRow = ws.Range("A1").End(xlDown).Row
colCount = ws.Range("A1").End(xlToRight).Column + 2

' Prepare the sorting column in wsSort
For n = 1 To colCount
    wsSort.Cells(1, n) = Chr(64 + n)
Next

' Write data to wsSort
For n = 2 To eRow
    Arry = Split(Trim(ws.Range("A" & n)), "-")
    k = -1
    For m = 1 To colCount
        If m < 4 Then
            k = k + 1
            wsSort.Cells(n, m) = Arry(k)
        Else
            wsSort.Cells(n, m) = ws.Cells(n, (m - 2))
        End If
    Next
Next

' Sort Data
wsSort.Range("A1", Chr(64 + colCount) & eRow).Select
With wsSort
    .Sort.SortFields.Clear
    For n = 1 To 3
        .Sort.SortFields.Add Key:=Range(Chr(64 + n) & "2", Chr(64 + n) & eRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    Next
End With

With wsSort.Sort
    .SetRange Range("A1", Chr(64 + colCount) & eRow)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

For n = 2 To eRow
    With ws
        .Range("A" & n) = wsSort.Range("A" & n) & "-" & wsSort.Range("B" & n) & "-" & wsSort.Range("C" & n)
        For m = 2 To colCount
            .Range(Chr(64 + m) & n) = wsSort.Range(Chr(66 + m) & n)
        Next
    End With
Next

Application.DisplayAlerts = False
wsSort.Delete
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Solution
Zot, thank you very much. This fix the problem perfectly.
Everyone on this forum are awesome.
Thank you again.

Alphix
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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