+ Reply to Thread
Results 1 to 4 of 4

Autofilter Sorting Macro

  1. #1
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Autofilter Sorting Macro

    Hello - I recorded a Macro to autofilter a range of cells with data in it. I then tried to transcribe the recorded macro into my own code, but am having difficulty understanding why it does not work. It returns the following error: "Run-time error '424': Object required'. To give you an idea of what the spreadsheet looks like, I have a list of headers in row 1. The data is listed in columns A - Q and I'd like to sort by column L from largest to smallest value.

    Please Login or Register  to view this content.
    Thanks in advance!
    Last edited by vba_beginner; 05-08-2013 at 05:06 PM.

  2. #2
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Autofilter Sorting Macro

    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:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Autofilter Sorting Macro

    According to another thread, you need to check if sheet is in AutoFilterMode? It seems to have done the trick! See "Andy Pope" answer:

    http://www.excelforum.com/excel-prog...e-not-set.html


    "Your code leaves the autofilter in place when either of the 2 routines runs.
    This means next time a routine is run the Autofilter command actually removes the autofilter which is why the clearing of the sort fails.
    You need to test for the autofilter being present are applying if not."


    Please Login or Register  to view this content.
    Last edited by vba_beginner; 05-08-2013 at 08:01 PM.

  4. #4
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Autofilter Sorting Macro

    In addition, if you're trying to repeat this sub for other data in the same sheet you may have noticed that the AutoFilter was still on for the initial range of cells selected. You should append

    Please Login or Register  to view this content.
    in the next line after "End With" in my sub so that when you re-run it for another Range of cells it'll work properly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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