Help with sort Macro

JohnCG

New Member
Joined
Jun 22, 2012
Messages
5
I am new to using macros and have been using the record macro feature to try to help but I can not figure out how to modify what excel recorded into what I would like to do.

Code:
Sub Macro13()
'
' Macro13 Macro
'

'
    Range("J2:J20").Select
    ActiveWorkbook.Worksheets("Calculator").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Calculator").Sort.SortFields.Add Key:=Range("J9") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Calculator").Sort
        .SetRange Range("J2:J20")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub


The issue is I do not know how to change the range because column J will not always end at J20, the real file I need to use this on varies but it typically ends at J320000. Is there a way to set the range until there are no more values in a cell within that column. Any help would really be appreciated.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You can add a variable called last row

Code:
Sub Macro13()
[COLOR="#FF0000"]    Dim rng As Range
    Dim LR As Long: LR = Range("J" & Rows.Count).End(xlUp).Row[/COLOR]    
    ActiveWorkbook.Worksheets("Calculator").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Calculator").Sort.SortFields.Add Key:=Range("J9") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Calculator").Sort
        .SetRange Range("J2:J" & [COLOR="#FF0000"][/COLOR][COLOR="#FF0000"]LR[/COLOR])
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Try :
Code:
Sub Mtest()
Range("J9:J" & Cells(Rows.Count, 10).End(xlUp).Row).Select
Selection.Sort _
Key1:=Range("J9"), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
 
Upvote 0

Forum statistics

Threads
1,212,046
Messages
6,105,582
Members
447,972
Latest member
carrieann

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