+ Reply to Thread
Results 1 to 10 of 10

quick sort macro

  1. #1
    Registered User
    Join Date
    04-25-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    24

    quick sort macro

    I find it very frustrating that the excel sort feature doesn't remember the last sort criteria, or have some sort of history list. It takes forever to have to re-enter sort criteria over and over when you need to repeat an operation.

    Can someone let me know how to write a macro that will sort whatever range of rows is currently selected. The column(s) to sort on and sort order will be the same every time and so can be hard coded in the macro. The range needs to be picked up from what ever is selected on the active sheet.

    Thanks,

    LMHmedchem

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: quick sort macro

    LM,

    Try using the macro recorder.

    Lewis

  3. #3
    Registered User
    Join Date
    04-25-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: quick sort macro

    I did use the macro recorder and this is what I end up with.

    Please Login or Register  to view this content.
    The recorder produces a script that will sort rows 254:262 in worksheet Sheet1. I need this to be more generic and sort whatever rows I have selected at the time when I run the macro. Ideally, I would just like to select some rows and keystroke the shortcut. I don't know VB anywhere near well enough to make those changes.

    LMHmedchem

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: quick sort macro

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    04-25-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: quick sort macro

    Quote Originally Posted by shg View Post
    Please Login or Register  to view this content.
    I'm not sure how you specify the column letter name to sort on. There will not be a header row in the selection, so it would need to be a column letter.

    LMHmedchem

  6. #6
    Registered User
    Join Date
    04-25-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: quick sort macro

    for some reason, my last reply didn't appear on the main board and it still looks like shg posted the last message.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: quick sort macro

    Did you try it, or are you speculating?

    It sorts on the first column of the data.

  8. #8
    Registered User
    Join Date
    04-25-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: quick sort macro

    Quote Originally Posted by shg View Post
    Did you try it, or are you speculating?

    It sorts on the first column of the data.
    Yes, it does sort on the first column with the ctrl+h keystroke (which I added), but most of the time I need to sort on a column other than the first. In this case I need to sort on the 6th column, small to large, but it would be nice if the script were generic so that I could edit it for different sort criteria.

    I didn't see anything obvious for where I could indicate the column to sort on.

    LMHmedchedm

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: quick sort macro

    In this case I need to sort on the 6th column
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    04-25-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: quick sort macro

    Quote Originally Posted by shg View Post
    Please Login or Register  to view this content.
    I thought that I tried that and got an error, maybe I changed the value of Key1 to Key6 instead.

    Thanks, that works just fine. What would the syntax be if I wanted to sort on more than one column, like 4 then 6?

    LMHmedchem

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: quick sort macro

    See Help for the Range.Sort method.

    At some point, it would be easier to just use the interface provided.

+ 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. quick sort question - vba
    By mrproject44 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-24-2013, 03:45 PM
  2. quick replace, sort, change columns
    By loade in forum Excel General
    Replies: 0
    Last Post: 04-18-2012, 02:47 PM
  3. Can I "auto-sort" or use a quick button to sort?
    By drewkloc71 in forum Excel General
    Replies: 4
    Last Post: 09-07-2009, 03:14 AM
  4. quick sort code
    By bceng in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-25-2008, 04:55 PM
  5. Quick way to sort lists by text color?
    By PokerZan in forum Excel General
    Replies: 3
    Last Post: 06-02-2005, 09:05 PM

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