Excel VBA Code Sort Columns A to AZ

Excel VBA Code Sort Columns A to AZ

November 12, 2017

Tonight I recorded a macro which will sort the range of columns on a worksheet from A to AZ - from left to right.    This VBA code replicates the sorting operation performed when a user sorts first by Column A, then at the next level by column B, then by column C on the third level, and so on.  

 

In this example we have a worksheet with the data entered in columns A to E.   

 

 

In order to sort each column from left to right (column A to column AZ) enter the below VBA code into a new module.   You'll need to find and replace the cell reference for the end of the data range (in this example on row 251), and also update the name of the worksheet you are sorting data on.

 

 

Running the macro produces this result:

 

 

 

 

Sub SortColumnsAtoAZ()
'
' SortColumnsAtoAZ Macro
'

'
    Cells.Select
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("A1:A251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("B1:B251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("C1:C251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("D1:D251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("E1:E251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("F1:F251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("G1:G251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("H1:H251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("I1:I251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("J1:J251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("K1:K251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("L1:L251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("M1:M251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("N1:N251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("O1:O251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("P1:P251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("Q1:Q251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("R1:R251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("S1:S251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("T1:T251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("U1:U251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("V1:V251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("W1:W251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("X1:X251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("Y1:Y251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("Z1:Z251") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AA1:AA251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AB1:AB251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AC1:AC251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AD1:AD251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AE1:AE251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AF1:AF251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AG1:AG251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AH1:AH251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AI1:AI251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AJ1:AJ251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AK1:AK251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AL1:AL251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AM1:AM251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AN1:AN251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AO1:AO251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AP1:AP251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AQ1:AQ251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AR1:AR251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AS1:AS251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AT1:AT251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AU1:AU251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AV1:AV251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AW1:AW251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AX1:AX251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AY1:AY251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Parks").Sort.SortFields.Add Key:=Range("AZ1:AZ251" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Parks").Sort
        .SetRange Range("A1:AZ251")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub


 

 

 

Please reload

Contact Me With Your Litigation Support Questions:

seankevinoshea@hotmail.com

  • Twitter Long Shadow

© 2015 by Sean O'Shea . Proudly created with Wix.com