macro to sort with variables for all ranges

Clintster

New Member
Joined
May 30, 2012
Messages
7
I cannot seem to get this to work. I have tried multple combinations, but keep getting the 1004 error. Help Please.

Sub RunSort(ColFirst, ColLast, RowFirst, RowLast)
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range(Cells(RowFirst, ColFirst), Cells(RowLast, ColLast)).Select
ActiveSheet.Sort.SortFields.Clear
Range(Cells(RowFirst, ColFirst), Cells(RowLast, ColLast)).Sort Key1:=Range(Cells(RowFirst, ColFirst)), Order1:=xlAscending, _
Key2:=Range(Cells(RowFirst, ColFirst + 1)), Order2:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

Ignore the "LAstrow", something else I was trying.
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Just use Cells for the keys
Code:
Key1:=Cells(RowFirst, ColFirst), Order1:=xlAscending, _
Key2:=Cells(RowFirst, ColFirst + 1)
 
Upvote 0
Thank you so much. this works.

Sub RunSort(ColFirst, ColLast, RowFirst, RowLast, SortRef)
Range(Cells(RowFirst, ColFirst), Cells(RowLast, ColLast)).Select
ActiveSheet.Sort.SortFields.Clear
Range(Cells(RowFirst, ColFirst), Cells(RowLast, ColLast)).Sort Key1:=Cells(RowFirst, ColFirst), Order1:=xlAscending, _
Key2:=Cells(RowFirst, ColFirst + 1), Order2:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,396
Messages
6,055,163
Members
444,767
Latest member
bryandaniel5

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