I seem to be getting an error 1004 about half the time when I run this chunk of code and I'm not at all sure why:
Dim ranged As Range
Set ranged = Range("AJ2")
Set ranged = Range(ranged, ranged.End(xlDown))
Sheets(i).Select
ActiveWorkbook.Worksheets(i).Sort.SortFields.Add Key:=ranged, _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(i).Sort
.SetRange ranged
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The code is being run over a series of worksheets and the range in column AJ is different in size over each sheet and therefore the range definition at the start is necessary. The aim is to reverse the order of the range. Can anyone help with what the error is here?