VBA - Sort Changing Input Range

whada

New Member
Joined
May 14, 2014
Messages
44
Hi There - I'm trying to sort data by two levels in VBA. The table doesn't have a firm number of rows though.
Here is where I got (code not working):

Dim LevelOne As Range
Dim LevelTwo As Range
Dim SortRange As Range


LevelOne = ActiveSheet.Range(Range("A4"), Range("A4").End(xlDown)).Select
LevelTwo = ActiveSheet.Range(Range("F4"), Range("F4").End(xlDown)).Select
SortRange = ActiveSheet.Range(Range("4:4"), Range("4:4").End(xlDown)).Select


ActiveWorkbook.Worksheets("Fcst_{Enterprise}").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Fcst_{Enterprise}").Sort.SortFields.Add Key:=LevelOne, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Fcst_{Enterprise}").Sort.SortFields.Add Key:=LevelTwo, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

With ActiveWorkbook.ActiveSheet.Sort
.SetRange SortRange
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With



Any help with this is greatly appreciated
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Just to add further clarification to this - I think the problem may be with the way I define these variables. Basically I have two columns A and F that I want to sort by. The number of rows in my table will go up or down so I wanted to use Range.End(xlDown) to find out what the sort are should be. I tried that above by using ActiveSheet.Range(Range("A4"), Range("A4").End(xlDown)).Select and then ActiveSheet.Range(Range("F4"), Range("F4").End(xlDown)).Select and then set the sort area as ActiveSheet.Range(Range("4:4"), Range("4:4").End(xlDown)).Select to select everything from row 4 down to the bottom but this code is not sorting at all...hm?
 
Upvote 0

Forum statistics

Threads
1,203,640
Messages
6,056,491
Members
444,869
Latest member
tulo spont

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