Sorting multiple columns help

bobby_smith

Board Regular
Joined
Apr 16, 2014
Messages
90
Good day,
I'm looking for an easy way to sort cross tabulated data by column.
I was able to get the desired results by using custom sort and adding each column at a time. This will not be efficient as I will have lots of columns.
Any feedback will be greatly appreciated.
See images for original ( before) and desired results (after)
 

Attachments

  • results.png
    results.png
    82.9 KB · Views: 8
  • before.png
    before.png
    25 KB · Views: 9

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
maybe use XL2BB to post the example data instead of HTML
and update your profile (Account details) about Excel version
 
Upvote 0
Updated and I'm using Excel 365
Before
test 1.xlsx
ABCDEFGHIJKLM
1ItemJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2Item 11,189.6020,568.1027,665.6412,640.55414,705.65
3Item 22,144.25
4Item 335,763.65214,571.14321,788.22631,022.27
5Item 41,353.311,477.5112,850.00
6Item 55,800.5648,815.3270,764.3823,455.8082,500.00
7Item 667.6212,897.404,314.16
8Item 74,224.004,291.62
9Item 81,389.641,737.05
10Item 92,978.9895,299.1193,118.9914,271.3910,465.8668,405.2644,517.1021,598.4539,438.7937,363.8755,101.46
11Item 10156,556.42207,944.9193,041.4348,014.575,928.6094,134.80
12Item 112,703.62922.73762.66
13Item 12593.18
14Item 131,617.005,219.637,504.012,814.1057,858.88
15Item 1421,780.00
16Item 152,897.1764,227.6363,831.5766,610.7361,531.8057,437.6135,422.4743,645.00
17Item 163,991.444,221.78
18Item 1712,443.428,096.1316,747.5947,092.19
Sheet1


After

test 1.xlsx
QRSTUVWXYZAAABAC
1ItemJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2Item 514,501.4122,038.3176,911.058,639.5206,250.0
3Item 97,447.5238,247.8232,797.535,678.526,164.7171,013.2111,292.853,996.198,597.093,409.7137,753.7
4Item 157,242.9160,569.1159,578.9166,526.8153,829.5143,594.088,556.2109,112.5
5Item 10391,391.1519,862.3232,603.6120,036.414,821.5235,337.0
6Item 43,383.33,693.832,125.0
7Item 83,474.14,342.6
8Item 134,042.513,049.118,760.07,035.3144,647.2
9Item 169,978.610,554.5
10Item 710,560.010,729.1
11Item 6169.132,243.510,785.4
12Item 12,974.051,420.369,164.131,601.41,036,764.1
13Item 1731,108.620,240.341,869.0117,730.5
14Item 389,409.1536,427.9804,470.61,577,555.7
15Item 116,759.12,306.81,906.7
16Item 121,483.0
17Item 25,360.6
18Item 1454,450.0
Sheet1
 
Upvote 0
Hi
May this help, your feed back is highly appreciated.

Thanks

VBA Code:
Sub Sorting_multiple_columns()

Dim Sht As Worksheet
Dim Rng As Range, MyRange As Range

Set Sht = ThisWorkbook.Worksheets("Sheet1")
Set MyRange = Worksheets("Sheet1").Range("A1:M18")
For Each Rng In MyRange.Cells(1, 2).Resize(1, MyRange.Columns.Count - 1)
'Count Empty Range
CER = WorksheetFunction.CountA(Range(Cells(lRow + 2, Rng.Column).Address & ":" & Cells(MyRange.Rows.Count, Rng.Column).Address))
If CER <> 0 Then

    Sht.sort.SortFields.Clear
    Sht.sort.SortFields.Add2 Key:=Rng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Sht.sort
        .SetRange Range("A" & IIf(lRow = "", 2, lRow + 1) & ":M20")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    lRow = Cells(Rows.Count, Rng.Column).End(xlUp).Row
    Rows(Cells(1, 1).Row & ":" & lRow).EntireRow.Hidden = True
End If

Next
MyRange.EntireRow.Hidden = False
MyRange.Offset(1, 0).EntireRow.Hidden = False
End Sub
sortmul.xlsm
ABCDEFGHIJKLM
1ItemJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2Item 11189.620568.127665.6412640.55414705.7
3Item 22144.25
4Item 335763.65214571.1321788.2631022.3
5Item 41353.311477.5112850
6Item 55800.648815.3270764.3823455.882500
7Item 667.6212897.44314.16
8Item 742244291.62
9Item 81389.641737.05
10Item 92978.9895299.19311914271.410465.8668405.2644517.121598.4539438.7937363.8755101.46
11Item 10156556.4207944.993041.448014.575928.694134.8
12Item 112703.62922.73762.66
13Item 12593.18
14Item 1316175219.637504.012814.157858.88
15Item 1421780
16Item 152897.1764227.6363831.5766610.761531.857437.6135422.4743645
17Item 163991.444221.78
18Item 1712443.428096.1316747.5947092.19
Sheet1


After

sortmul.xlsm
ABCDEFGHIJKLM
1ItemJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2Item 55800.648815.3270764.3823455.882500
3Item 92978.9895299.19311914271.410465.8668405.2644517.121598.4539438.7937363.8755101.46
4Item 152897.1764227.6363831.5766610.761531.857437.6135422.4743645
5Item 10156556.4207944.993041.448014.575928.694134.8
6Item 41353.311477.5112850
7Item 81389.641737.05
8Item 1316175219.637504.012814.157858.88
9Item 163991.444221.78
10Item 742244291.62
11Item 667.6212897.44314.16
12Item 11189.620568.127665.6412640.55414705.7
13Item 1712443.428096.1316747.5947092.19
14Item 335763.65214571.1321788.2631022.3
15Item 112703.62922.73762.66
16Item 12593.18
17Item 22144.25
18Item 1421780
Sheet1
 
Upvote 0
@Dossfm0q

Thank you very much, the code works just as I needed.

To anyone reusing this code for, you will need to make changes to the items in red box in the attached pic.
Change 1 to the name of the current sheet with the data to me sorted
Change 2 to the name of the current sheet with the data to me sorted
Change 3 to range the data to be sorted in
Change 4 to the last cell in your range
 

Attachments

  • code.png
    code.png
    44 KB · Views: 8
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

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