After much trial and error, I was able to fix the code such that when I run it, the table of data is sorted by column L in descending order! However, when I re-run the sub, I get an error msg again: "Run-time error '91': Object variable or With block variable not set". If I select 'End' from the pop up error message box, and then re-run the sub, it works fine. Run it again, and the error pops up... (you see the pattern here).
I would have thought the .Sort.SortFields.Clear portion would have cleared any sort, which I presume is the ultimate error. Any ideas? See corrected code below:
Sub AutoFilter()
Range("A83:Q83").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Portfolio by Week").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Portfolio by Week").AutoFilter.Sort.SortFields.Add _
Key:=Range("L83"), _
SortOn:=xlSortOnValues, _
Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Portfolio by Week").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Bookmarks