SOLVED

VBA Error (but only on one sheet): Object variable or With block variable not set

Copper Contributor

I'm using Excel in Microsoft Office 365 on Windows 10. I've been using a macro to sort two tabs on my spreadsheet, which are identical in format, and after months of working perfectly, now the macro is not working in Tab A of the spreadsheet even though it works fine in Tab B. Whenever I try to run it, I receive the following error: "Run-time error '91': Object variable or With block variable not set"

 

I tried the "Debug" feature and it highlighted the first line of my code:

 

 

 

ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear

 

 

 

Oddly, as I was trying to fix this, even when I deleted this section of the macro, it just kept highlighting the first line of my code, no matter what that first line was. I'm a total newbie when it comes to debugging, though, so maybe there's something else I need to do to find the correct string of code that's causing the issue?

 

In any event, I have no idea what the problem is because this macro is working perfectly in Tab B. I have not made any changes to Tab A apart from the usual data entry - the format is still the same, no added or removed columns, etc.

 

I've pasted the full code below.

 

 

 

Sub WritingProjectSort()
'
' WritingProjectSort Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
    ActiveSheet.AutoFilter.Sort.SortFields.Clear
    ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key _
        :=Range("AA1:AA252"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.AutoFilter.Sort.SortFields.Clear
    ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key _
        :=Range("R1:R252"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.AutoFilter.Sort.SortFields.Clear
    ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key _
        :=Range("N1:N252"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.AutoFilter.Sort.SortFields.Clear
    ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key _
        :=Range("F1:F252"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range(Selection, Selection.End(xlUp)).Select
End Sub

 

 

 

(If it helps to know what I'm trying to do here, I have a list of writing projects that are undergoing various types of review and have all been sent out on different dates. I am trying to organize primarily by review type (Column F), then the date the project was sent for review (Columns N, R, and AA depending on the review type), and finally alphabetical order by writing project name (Column A) so that any project not undergoing review will be listed alphabetically after all the others undergoing review.)

 

Thank you very much for any help you can give me.

1 Reply
1 best response