Sort Macro

skhou

Board Regular
Joined
May 22, 2007
Messages
83
Hi,

I have the following VB code whereby I am sorting the columns B, then columns A by alphabetical order. The current code below works fine because the range has been hardcoded to be 53 rows. However, the rows will always change, so I am just wondering how I can tweak the code so it recognises to capture whatever the current number of rows is at the time? Thanks

Sub Sort()

ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort.SortFields.Add _
Key:=Range("B2:B53"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal


ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort.SortFields.Add _
Key:=Range("A2:A53"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal


With ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort
.SetRange Range("A1:B53")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This will reference A2 to the last used row in column A

Code:
Sub Sort()

    [COLOR="Red"]Dim Lastrow As Long
    
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row[/COLOR]
    
    ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort.SortFields.Add _
            Key:=Range([COLOR="Red"]"B2:B" & Lastrow[/COLOR]), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal


    ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort.SortFields.Add _
            Key:=Range([COLOR="Red"]"A2:A" & Lastrow[/COLOR]), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal


    With ActiveWorkbook.Worksheets("Entity_Identifier_Function").Sort
        .SetRange Range([COLOR="Red"]"A1:A" & Lastrow[/COLOR])
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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