I have the following code:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=ActiveCell.Range _
("A1:A298"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 4).Range("A1:A298"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange ActiveCell.Offset(-1, 0).Range("A1:E299")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Which runs fine in this particular worksheet but I have data that will be ending at different row numbers.
I added:
Dim LastRow As Integer 'This is the last non empty row
LastRow = ActiveSheet.UsedRange.Row - 1 + _ ActiveSheet.UsedRange.Rows.Count
Then used "LastRow" in the Range fields. Now I am getting a Run error of 1004: 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.
What am I doing wrong?
Thanks
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=ActiveCell.Range _
("A1:A298"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 4).Range("A1:A298"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange ActiveCell.Offset(-1, 0).Range("A1:E299")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Which runs fine in this particular worksheet but I have data that will be ending at different row numbers.
I added:
Dim LastRow As Integer 'This is the last non empty row
LastRow = ActiveSheet.UsedRange.Row - 1 + _ ActiveSheet.UsedRange.Rows.Count
Then used "LastRow" in the Range fields. Now I am getting a Run error of 1004: 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.
What am I doing wrong?
Thanks