Hi,
im trying to sort a variable range which will be different everytime i use the sheet, may be more columns and may be more rows, i have managed with vba to select the variable range everytime, but then when i try to sort it by cell color, i get error that VBA has a problem with (.apply)
here is the code i use to select and define the variable range and then try to sort it i get this error "the sort reference is not valid. make sure that it's within the data you want to sort, and the first sort by box isnt the same or blank"
im trying to sort a variable range which will be different everytime i use the sheet, may be more columns and may be more rows, i have managed with vba to select the variable range everytime, but then when i try to sort it by cell color, i get error that VBA has a problem with (.apply)
here is the code i use to select and define the variable range and then try to sort it i get this error "the sort reference is not valid. make sure that it's within the data you want to sort, and the first sort by box isnt the same or blank"
Code:
Range("O2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Dim LoadList As Range
Set LoadList = Selection
LoadList.Select
ActiveWorkbook.Worksheets("Anycompare").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Anycompare").Sort.SortFields.Add Key:=Range( _
"LoadList"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Anycompare").Sort.SortFields.Add Key:=Range( _
"LoadList"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Anycompare").Sort
.SetRange LoadList
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With