Multiple Sort - move columns in order

techgirl

Board Regular
Joined
Sep 16, 2002
Messages
178
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a file that has Row AND Column titles. Both are out of order and both need to be sorted, and being able to keep the data together.
I would like to have the Names in column one in alphabetical order, as well as the Client names in alphbetical order. I have over 60 columns and don't want to have to drag and drop in order.

Client 3Client 1Client 10Client 2
Bob
5-Mar-22​
1-Jan-22​
2-Feb-22​
1-Jan-22​
Sandy
10-Feb-22​
6-Mar-22​
2-Mar-22​
1-Jan-22​
Eric
5-Jan-22​
1-Mar-22​
15-Jan-22​
5-Jan-22​
Autumn
2-Feb-22​
11-Feb-22​
2-Feb-22​
2-Feb-22​
Andrew
1-Jan-22​
5-Mar-22​
10-Feb-22​
5-Mar-22​
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this macro:
VBA Code:
Public Sub Sort_Rows_and_Columns()

    Dim lastCol As Long, lastRow As Long
    Dim colsRange As Range, rowsRange As Range
    
    With ThisWorkbook.ActiveSheet
    
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Set colsRange = .Range("B1").Resize(lastRow, lastCol - 1)
        Set rowsRange = .Range("A2").Resize(lastRow - 1, lastCol)
    
        'Sort columns
    
        .Sort.SortFields.Clear
        .Sort.SortFields.Add2 Key:=colsRange.Resize(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange colsRange
            .Header = xlYes 'xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
   
        'Sort rows
    
        .Sort.SortFields.Clear
        .Sort.SortFields.Add2 Key:=rowsRange.Resize(, 1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange rowsRange
            .Header = xlNo 'xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
   
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,517
Members
452,921
Latest member
BBQKING

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