Slight Tweak To Recorded Macro

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have recorded a sort macro. What I need to do is change where it says range C5880:C5898, AK5880:AK5898 etc. Can it be selected range of rows I choose as they will be different each time I use? Thanks

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 key:=Range( _
        "C5880:C5898"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 key:=Range( _
        "AK5880:AK5898"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 key:=Range( _
        "AF5880:AF5898"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A5880:AY5898")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Not sure where the column A sort comes into it though as I didn't choose that in my recorded macro?
Not sure what you are talking. Are you talking about rng4?

Note in your original code:
VBA Code:
     .SetRange Range("A5880:AY5898")

Note in my code (the replacement for this):
VBA Code:
    Set rng4 = Range("A" & fr & ":AY" & lr)
then
VBA Code:
        .SetRange rng4

This is the ENTIRE range you want to sort (that is necessary for any sorting!).
Note it is columns "A" to "AY", not just column "A"!
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Not sure what you are talking. Are you talking about rng4?

Note in your original code:
VBA Code:
     .SetRange Range("A5880:AY5898")

Note in my code (the replacement for this):
VBA Code:
    Set rng4 = Range("A" & fr & ":AY" & lr)
then
VBA Code:
        .SetRange rng4

This is the ENTIRE range you want to sort (that is necessary for any sorting!).
Note it is columns "A" to "AY", not just column "A"!
Understood, thanks squire.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

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