Build a Sort Dynamically

Mitchbvi

New Member
Joined
Mar 6, 2014
Messages
39
I am trying to work out how to build a sort when I do not know at the outset how many columns I need to sort on.

The first object was to see if I could pass the range and sort key dynamically. The following Macro does that getting range and column to sort by. When I try and build the entire sort string I get a message 1004. I included a MsgBox to make sure the sort string is correct and it seems to be.

Code:
Sub Range_Sort()
Dim SortT, SortS, SortRange, KeyCell1, KeyCell2, SortString
Call LastCell
Set SortRange = Range(Cells(1, 1), Cells(LROW, LCOL))
Set SortT = Application.InputBox("Select to sort", , Type:=8)
Set KeyCell1 = Range(SortT.Address)
'This Works
SortRange.Sort key1:=KeyCell1, Order1:=xlDescending
'This does not work
SortString = "key1:=KeyCell1, Order1:=xlDescending"
MsgBox SortString
SortRange.Sort SortString
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Why are you trying to create a string for the sort when this line works?
Code:
SortRange.Sort key1:=KeyCell1, Order1:=xlDescending
 
Upvote 0
Thank you for your repponse, I apologise I should have. made it clear, that at the outset I do not know how ,many columns I need to sort on. however I have solved the sort issue in the following macro. To me it still seems. clumsy however I have included it in case it is of interest to others.

Sub SortColumns()
'Sorts 1 or multiple Columns in ascending Order
Dim DataRange, SortCols, X, Y
Call LastCell
DataRange = Range(Cells(1, 1), Cells(LROW, LCOL))
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
SortCols = Application.InputBox("Enter No columns to Sort")
X = 1
For Y = 1 To SortCols 'Adds Sort keys
KeyCell = KeyCell & X
Set KeyCell = Application.InputBox("Select Column " & X & " to sort", , Type:=8)
KeyCell = KeyCell.Address
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range(KeyCell), Order:=xlAscending
X = X + 1
Next Y
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("DataRange")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Upvote 0
Glad you sorted it & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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