tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,759
- Office Version
- 365
- 2019
- Platform
- Windows
I want to sort three columns of data.
The code fails on this line:
with the message:
Can someone tell me what's wrong?
Recording a macro gives this:
which works.
Thanks
Code:
Dim rng As Range
Set rng = Sheet1.Range("C10:E100")
With Sheet1.Sort.SortFields
.Clear
.Add2 Key:=rng, _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
End With
With Sheet1.Sort
.SetRange rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set rng = Nothing
The code fails on this line:
Code:
.Apply
with the message:
Code:
The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.
Can someone tell me what's wrong?
Recording a macro gives this:
Code:
Range("C10:E100").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"C10:E100"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("C10:E100")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
which works.
Thanks