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