+ Reply to Thread
Results 1 to 9 of 9

Run Time Error '91: Object variable or With block variable not set

  1. #1
    Registered User
    Join Date
    09-10-2019
    Location
    Washington,DC
    MS-Off Ver
    365
    Posts
    5

    Run Time Error '91: Object variable or With block variable not set

    I have a simple macro that I made with the "record macro" feature that pulls in data from a different tab and then paste is as values to remove all links and then sorts it by one of the columns. For some reason I keep getting an error that says

    Run Time Error '91:
    Object variable or With block variable not set

    When I hit the "debug" error it highlights this part of the code:

    ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort. _
    SortFields.Clear



    The entire macro is below. Any ideas on what I did wrong or how to fix? Thank you in advance!



    Sub Closest_Expiration_Dates()
    '
    ' Closest_Expiration_Dates Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+E
    '
    Sheets("Condensed NSAs").Select
    Selection.Copy
    Sheets("Closest Expiration Dates").Select
    ActiveSheet.Paste
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("A1:R1").Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort. _
    SortFields.Clear
    ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort. _
    SortFields.Add2 Key:=Range("R1"), SortOn:=xlSortOnValues, Order:= _
    xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort. _
    SortFields.Clear
    ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort. _
    SortFields.Add2 Key:=Range("R1"), SortOn:=xlSortOnValues, Order:= _
    xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    ActiveWindow.SmallScroll Down:=-3
    End Sub

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Run Time Error '91: Object variable or With block variable not set

    Try changing this line
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-10-2019
    Location
    Washington,DC
    MS-Off Ver
    365
    Posts
    5

    Re: Run Time Error '91: Object variable or With block variable not set

    Thanks. Gave it a shot and I still get the error. Appreciate your help.

  4. #4
    Registered User
    Join Date
    09-10-2019
    Location
    Washington,DC
    MS-Off Ver
    365
    Posts
    5

    Re: Run Time Error '91: Object variable or With block variable not set

    The debugger keeps highlighting this line:
    ActiveWorkbook.Worksheets("Closest Expiration Dates").AutoFilter.Sort. _
    SortFields.Clear

  5. #5
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Run Time Error '91: Object variable or With block variable not set

    Can you post a safe version of your workbook?

    Go Advanced > Manage Attachments...

  6. #6
    Registered User
    Join Date
    09-10-2019
    Location
    Washington,DC
    MS-Off Ver
    365
    Posts
    5

    Re: Run Time Error '91: Object variable or With block variable not set

    Unfortunately, I cannot post the workbook.

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,075

    Re: Run Time Error '91: Object variable or With block variable not set

    try replacing
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    Also please edit you original post to include code tags, the # icon in the reply/edit window.

  8. #8
    Registered User
    Join Date
    09-10-2019
    Location
    Washington,DC
    MS-Off Ver
    365
    Posts
    5

    Re: Run Time Error '91: Object variable or With block variable not set

    Thanks. That did something strange and it started to put the data off to the side rather than refresh the data.

    Also, I believe I've updated the post to include the code tags. Let me know if I did wrong. Thank you.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,075

    Re: Run Time Error '91: Object variable or With block variable not set

    How about
    Please Login or Register  to view this content.
    The code tags in your op have not worked. If they were applied correctly the code would be in a box, like the code in this post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Getting run-time error 91 object variable or with block variable not set
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2016, 09:46 PM
  2. [SOLVED] Getting run-time error 91 object variable with block variable not set
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2016, 10:14 PM
  3. [SOLVED] Run-time error '91': Object variable or With block variable not set when closing userform
    By bishoposiris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2016, 08:59 AM
  4. Replies: 0
    Last Post: 04-16-2013, 07:15 AM
  5. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  6. Replies: 1
    Last Post: 09-25-2012, 08:03 PM
  7. [SOLVED] Intermittent Run-time Error 91: Object Variable or With Block variable not set
    By fraanchtoast in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-29-2012, 10:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1