Change Recorded Macro To Used Range

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have recorded the macro below but I want to use it on different files with different amount of rows. What do I need to do for it to work on a used range rather than a set amount of rows like below please? Also the name of the sheet to be active sheet rather than a named sheet.

Code:
Sub Macro5()
    ActiveWorkbook.Worksheets("Sheet").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet").Sort.SortFields.Add2 Key:=Range("A2:A2456" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet").Sort
        .SetRange Range("A1:T2456")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I have recorded the macro below but I want to use it on different files with different amount of rows. What do I need to do for it to work on a used range rather than a set amount of rows like below please? Also the name of the sheet to be active sheet rather than a named sheet.

Code:
Sub Macro5()
    ActiveWorkbook.Worksheets("Sheet").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet").Sort.SortFields.Add2 Key:=Range("A2:A2456" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet").Sort
        .SetRange Range("A1:T2456")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub
For Name of active sheet - instead of .Worksheets("Sheet") Change to .ActiveSheet

for variable number of rows - use structured tables, in that case the range becomes the TableName and you can sort values by any column head you wish to

Even that would have limitation if table name changes in every file.
 
Upvote 0
For Name of active sheet - instead of .Worksheets("Sheet") Change to .ActiveSheet

for variable number of rows - use structured tables, in that case the range becomes the TableName and you can sort values by any column head you wish to

Even that would have limitation if table name changes in every file.
'
for variable number of rows - use structured tables, in that case the range becomes the TableName and you can sort values by any column head you wish to

Even that would have limitation if table name changes in every file.'

Sorry don't understand any of this.
 
Upvote 0
'
for variable number of rows - use structured tables, in that case the range becomes the TableName and you can sort values by any column head you wish to

Even that would have limitation if table name changes in every file.'

Sorry don't understand any of this.
Convert your data on a sheet to a table using Insert Tab - Insert Table

This converts your data to structured table. remember to name that table in Table Design Tab.

Hope that helps
 
Upvote 0
I thought it may be something similar to below but I don't know where to put it etc,,,

lr = .Range("A" & .Rows.Count).End(xlUp).Row
 
Upvote 0
I thought it may be something similar to below but I don't know where to put it etc,,,

lr = .Range("A" & .Rows.Count).End(xlUp).Row
try this

Rich (BB code):
Dim lr as Range

ActiveWorkbook.Activesheet.Sort.SortFields.Clear

ActiveWorkbook.Activesheet.Sort.SortFields.Add2 Key:=Range("lr" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet").Sort
        .SetRange Range("lr")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
try this

Rich (BB code):
Dim lr as Range

ActiveWorkbook.Activesheet.Sort.SortFields.Clear

ActiveWorkbook.Activesheet.Sort.SortFields.Add2 Key:=Range("lr" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet").Sort
        .SetRange Range("lr")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
I don't think that will work at all, as "lr" is just a single row number, not a range address.
 
Upvote 0
try this

Rich (BB code):
Dim lr as Range

ActiveWorkbook.Activesheet.Sort.SortFields.Clear

ActiveWorkbook.Activesheet.Sort.SortFields.Add2 Key:=Range("lr" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet").Sort
        .SetRange Range("lr")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
That didn't work. When I debug it points to..
ActiveWorkbook.Worksheets("Sheet").Sort.SortFields.Add2 Key:=Range("lr" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
 
Upvote 0
Don't worry about it, I will just change the numbers in the recorded code.
 
Upvote 0
Does this do what you want?
VBA Code:
Sub MySort()

    Dim lr As Long
    Dim rng1 As Range
    Dim rng2 As Range
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Set ranges
    Set rng1 = Range("A2:A" & lr)
    Set rng2 = Range("A1:T" & lr)
    
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add2 Key:=rng1, _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange rng2
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,239
Messages
6,123,818
Members
449,127
Latest member
Cyko

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