VBA sorting columns with unknown number of rows

LNG2013

Active Member
Joined
May 23, 2011
Messages
465
I need help I have Data on sheet "Data" in Columns A:AD with an undetermined number of rows.
In the code I recorded below it says A2:A52 but it changes depending on the data that is imported
Columns are C, E, F, and I.



VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets("Data").Select
    Cells.Select
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("C2:C52"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("E2:E52"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("F2:F52"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("I2:I52"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("A1:AD52")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
See if this works for you.

VBA Code:
Sub SortMacro1()
'
' Macro1 Macro
'

'
    Dim shtData As Worksheet
    Dim lastRow As Long
    
    Set shtData = Worksheets("Data")
    lastRow = shtData.Range("A" & Rows.Count).End(xlUp).Row

    With shtData
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("C2:C" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("E2:E" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("F2:F" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("I2:I" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
    
    With shtData.Sort
        .SetRange Range("A1:AD" & lastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Solution
See if this works for you.

VBA Code:
Sub SortMacro1()
'
' Macro1 Macro
'

'
    Dim shtData As Worksheet
    Dim lastRow As Long
   
    Set shtData = Worksheets("Data")
    lastRow = shtData.Range("A" & Rows.Count).End(xlUp).Row

    With shtData
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("C2:C" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("E2:E" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("F2:F" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("I2:I" & lastRow), _
              SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
   
    With shtData.Sort
        .SetRange Range("A1:AD" & lastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
That works ty!
 
Upvote 0

Forum statistics

Threads
1,215,807
Messages
6,127,005
Members
449,351
Latest member
Sylvine

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