Sort Range Macro Question

rjacmuto32

Board Regular
Joined
Jul 14, 2004
Messages
98
I have a set of data that I want to put a macro to Sort the data by 2 columns. The twist is each time I will run the macro, my data will have a different ending Column # and Row # so I can't do a defined Ranger. I selected my starting cell A2 in the code below, then put the XL Right and Down to select all the data before it's sorted. Below that is my Sorting Code for the 2 columns. what I noticed is in the Sort it has =Range("E3:E8"). How do you fix that to be eqaul the range I'm selecting in the first part of the code? Cell E3 contains the first value to be sorted (E2 is the header). How do you make it go to the bottom of the range and not stop at row 8?

THanks,
--Robert



Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E3:E8"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F3:F8"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
 

Some videos you may like

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

JSA715

Active Member
Joined
Aug 3, 2008
Messages
254
without testing it myself, have you tried adding the "Range(Selection, Selection.End(xlDown))" in place of the Range("E3:E8")? Or setting a range variable to "Range(Selection, Selection.End(xlDown))" and then putting that variable in the sort line?
 

Watch MrExcel Video

Forum statistics

Threads
1,122,511
Messages
5,596,576
Members
414,079
Latest member
Frills

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
Top