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.
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