sorting in 2007

shadow12345

Well-known Member
Joined
May 10, 2004
Messages
1,238
Hi,

Im sure this is simple but I can't work it out. All i am try to do is record my sort in VBA. I am selecting with ctrl + * and applying the sort, but the range can change so I tried to edit it.

This is what i got from the recorder
Code:
ActiveWorkbook.Worksheets("Source data").sort.SortFields.Add Key:=Range("B2:B1500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=    xlSortTextAsNumbers

I changed it to this (range selection first to last row i think),

Code:
lst = Range("A" & Rows.Count).End(xlUp).Row
    ActiveWorkbook.Worksheets("Source data").sort.SortFields.Add Key:=Range("$B$2:$B$" & 1st), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=    xlSortTextAsNumbers

But its not working.

How can i make the first code look at all used cells?




**** If it is needed this is the whole code *****

Code:
  Cells.Select
    ActiveWorkbook.Worksheets("Source data").sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Source data").sort.SortFields.Add Key:=Range( _
        "B2:B5123"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    ActiveWorkbook.Worksheets("Source data").sort.SortFields.Add Key:=Range( _
        "W2:W5123"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Source data").sort.SortFields.Add Key:=Range( _
        "A2:A5123"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Source data").sort
        .SetRange Range("A1:AF5123")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("E2").Select
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I don't think 1st is a valid variable name,

It works for me if I change 1st to LR
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,802
Members
452,943
Latest member
Newbie4296

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