Add Sort Function To A Varying Length Spreadsheet

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to add a sort function to a spreadsheet that varies in length, however the code stops when it gets to the .Apply section, does anyone know how to fix this?

See the code below:
Code:
LastRow = sht.Range("A1").CurrentRegion.Rows.Countsht.Range("A2", sht.Range("k" & LastRow)).Select
'
ActiveWorkbook.Worksheets("RawData").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("RawData").Sort.SortFields.Add Key:=Range( _
        "A2" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("RawData").Sort.SortFields.Add Key:=Range( _
        "D2" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("RawData").Sort.SortFields.Add Key:=Range( _
        "C2" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("RawData").Sort.SortFields.Add Key:=Range( _
        "G2" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("RawData").Sort.SortFields.Add Key:=Range( _
        "E2" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("RawData").Sort
        .SetRange Range("A2" & LastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


Any help you can give on this would be greatly appreciated

Thanks in advance
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Change the Keys to read A1, D1 etc
and then use
Code:
With ActiveWorkbook.Worksheets("RawData").Sort
    .SetRange Range("[COLOR=#ff0000]A2:K" & LastRow[/COLOR])
    .Header = [COLOR=#ff0000]xlYes[/COLOR]
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 
Upvote 0
Excellent work thank you for this, its so obvious now you've said that much appreciated.

Change the Keys to read A1, D1 etc
and then use
Code:
With ActiveWorkbook.Worksheets("RawData").Sort
    .SetRange Range("[COLOR=#ff0000]A2:K" & LastRow[/COLOR])
    .Header = [COLOR=#ff0000]xlYes[/COLOR]
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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