+ Reply to Thread
Results 1 to 8 of 8

Need help with macro to auto sort based on two things

  1. #1
    Registered User
    Join Date
    02-08-2017
    Location
    Somerset, NJ
    MS-Off Ver
    2016
    Posts
    5

    Need help with macro to auto sort based on two things

    Hey everyone.

    I have a question. I hope someone can help me out or point me in the right direction. I've tried searching for hours but can't seem to find anything.

    I have an excel file at work with thousands of rows and about a dozen columns. I add rows and edit some continuously through out the day and would like to add a macro that basically runs every time there is a change in one column. From looking around I found a macro that can run as soon as a change in a specific column takes place but I have no idea on how to make it so that it sorts by 2 different criteria.

    If anyone could help me, my goal is to have the macro run and sort by these two criteria:
    1. Sort column G on values from A to Z. The title for Column G in the table is "Arrived".
    and then
    2. Sort column H on values from newest to oldest. The title for column H in the table is "ETA".


    I am completely new here. I've only recored a few macros in the past so if someone could please help me out that would be AMAZING.

    Thanks in advance!

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with macro to auto sort based on two things

    smanonfire,

    You said you want it to sort whenever one column changes, but we don't know which column? If you already have code to sort for one criteria, then I assume is looks like

    Range("somerange").Sort ... to use two criteria, you would:

    Please Login or Register  to view this content.
    This assumes that row 1 is the first row and you have headers. If the header row is a different one just change the 1 in G1 and H1. Also, it assumes H has some kind of age value, if it is dates then you may have to change xlAscending to xlDescending
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Need help with macro to auto sort based on two things

    Use the macro recorder to run through sorting the columns. That code is the meat of what you need.

    You'll want to use the Worksheet_Change Event trigger, set to intersect the targeted columns you care about. Event code goes directly on the worksheet module it's being applied to (Shortcut: right-click the tab and View Code)

    Please Login or Register  to view this content.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  4. #4
    Registered User
    Join Date
    02-08-2017
    Location
    Somerset, NJ
    MS-Off Ver
    2016
    Posts
    5

    Re: Need help with macro to auto sort based on two things

    Arkadi,

    I'm sorry. I totally forgot to include that. I'm looking to have it run as soon as I make a change in column G ("Arrived")

  5. #5
    Registered User
    Join Date
    02-08-2017
    Location
    Somerset, NJ
    MS-Off Ver
    2016
    Posts
    5

    Re: Need help with macro to auto sort based on two things

    Arkadi,

    I'm sorry. I totally forgot to include that. I'm looking to have it run as soon as I make a change in column G ("Arrived")

  6. #6
    Registered User
    Join Date
    02-08-2017
    Location
    Somerset, NJ
    MS-Off Ver
    2016
    Posts
    5

    Re: Need help with macro to auto sort based on two things

    daffodil11,

    I ran the macro recorder and came up with the following after running the sort:

    Sub Autosort()
    '
    ' Autosort Macro
    '
    ' Keyboard Shortcut: Option+Cmd+j
    '
    ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort.SortFields. _
    Clear
    ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort.SortFields.Add _
    Key:=Range("Table2[Arrived]"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort.SortFields.Add _
    Key:=Range("Table2[ETA]"), SortOn:=xlSortOnValues, Order:=xlDescending, _
    DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End Sub


    Now, how exactly do I set up the change event trigger to run every time I make a change in column G?

  7. #7
    Registered User
    Join Date
    02-08-2017
    Location
    Somerset, NJ
    MS-Off Ver
    2016
    Posts
    5

    Re: Need help with macro to auto sort based on two things

    daffodil11,

    I ran the macro recorder and came up with the following after running the sort:

    Sub Autosort()
    '
    ' Autosort Macro
    '
    ' Keyboard Shortcut: Option+Cmd+j
    '
    ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort.SortFields. _
    Clear
    ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort.SortFields.Add _
    Key:=Range("Table2[Arrived]"), SortOn:=xlSortOnValues, Order:=xlAscending _
    , DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort.SortFields.Add _
    Key:=Range("Table2[ETA]"), SortOn:=xlSortOnValues, Order:=xlDescending, _
    DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Monthly").ListObjects("Table2").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End Sub


    Now, how exactly do I set up the change event trigger to run every time I make a change in column G?

  8. #8
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Need help with macro to auto sort based on two things

    Always be sure to use code brackets in your code in posts. (Just highlight the text and click the # sign on the toolbar)

    Please Login or Register  to view this content.

+ 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. Macro to auto sort rows based on data changes
    By jasonfmerritt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-29-2016, 07:43 PM
  2. Replies: 4
    Last Post: 06-09-2016, 01:30 PM
  3. [SOLVED] Sort macro doing squirley things to my spreadsheet!
    By amoraali in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2015, 05:37 PM
  4. [SOLVED] Auto Sort Macro Based on Two Columns
    By Zabalba in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-22-2012, 12:04 PM
  5. Auto-Generate Due Date based on Ship Date, and other things
    By cjmorin in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-18-2011, 02:51 PM
  6. Auto number cells plus other things
    By Dark Adro in forum Excel General
    Replies: 2
    Last Post: 05-02-2008, 09:25 PM
  7. How to easily sort things
    By chojje in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 06-14-2007, 08:50 AM

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