Sorting with variable key VBA

EduG

New Member
Joined
Nov 19, 2009
Messages
3
I want the user of my spreadsheet to tell me which columns should be used to sort the data. e.g. column A and F. So I defined variables and try to use the variabel in my sort function. But it is not recognizing the variables in my sort function.
Here is my code (cell A2 = A, cell B2 = F).


Dim Col1 As Range
Set Col1 = Worksheets("Settings").Range("A2")
Dim Col2 As Range
Set Col2 = Worksheets("Settings").Range("B2")

Sheets("test1").Select
Range("A2:AX1000").Select
ActiveWorkbook.Worksheets("test1").Sort.SortFields.Add Key:=Range(Col1) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("test1").Sort.SortFields.Add Key:=Range(Col2) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("test1").Sort
.SetRange Range("A2:AX1000")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this

Code:
Dim Col1 As Variant
Col1 = Worksheets("Settings").Range("A2").Value2
Dim Col2 As Variant
Col2 = Worksheets("Settings").Range("B2").Value2

With ActiveWorkbook.Worksheets("test1")

    .Sort.SortFields.Add _
        Key:=.Range(Col1), _
        SortOn:=xlSortOnValues, _
        Order:=xlAscending, _
        DataOption:=xlSortNormal
    .Sort.SortFields.Add _
        Key:=.Range(Col2), _
        SortOn:=xlSortOnValues, _
        Order:=xlAscending, _
        DataOption:=xlSortNormal
    With .Sort
        .SetRange .Range("A2:AX1000")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With
 
Upvote 0
I tried both options. Andrews options sorts the wrong columns, xld solution gives an error.

If I replace the col1 by "A2" and col2 by "F2" in my programming than it works fine. So the trick must be in creating the variables col1 and col2.
 
Upvote 0
Thanks for the help, that plus some other minor changes did the trick. For future reference, here is the code I used.

Dim Col1 As Range
Set Col1 = Worksheets("Settings").Range("A2")
Dim Col2 As Range
Set Col2 = Worksheets("Settings").Range("B2")

Sheets("test1").Select
Range("A2:AX1000").Select
ActiveWorkbook.Worksheets("test1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("test1").Sort.SortFields.Add Key:=Range(Col1.Value & 2) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("test1").Sort.SortFields.Add Key:=Range(Col2.Value & 2) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("test1").Sort
.SetRange Range("A2:AX1000")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,711
Messages
6,126,401
Members
449,312
Latest member
sweetfriend9

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