+ Reply to Thread
Results 1 to 6 of 6

auto sort automatically on save

  1. #1
    WylieDude
    Guest

    auto sort automatically on save

    Is there a way I can have my sheet autosort selected rows when I re-save the
    sheet after adding new data.

    Thanks in Advance



  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You can use a before save event macro



    eg

    this code goes on the Workbook module sheet and sort columns a & b of the active sheet

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Columns("A:B").Sort Key1:=Range("A1"), _
    Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    End Sub

  3. #3
    WylieDude
    Guest

    Re: auto sort automatically on save

    With that sample I get "Compile Error Expected: type name

    You'll have to excuse me as I am relatively new to trying to mess with
    excel. Any help appreciated. Thanks in Advance

    Here is the code I am trying:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
    Boolean)
    Columns("C").Sort Key1:=Range("C12:C20"), _
    Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    End Sub

    "mudraker" <[email protected]> wrote in
    message news:[email protected]...
    >
    > You can use a before save event macro
    >
    >
    >
    > eg
    >
    > this code goes on the Workbook module sheet and sort columns a & b of
    > the active sheet
    >
    > Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
    > Boolean)
    > Columns("A:B").Sort Key1:=Range("A1"), _
    > Order1:=xlAscending, Header:=xlGuess, _
    > OrderCustom:=1, MatchCase:=False, _
    > Orientation:=xlTopToBottom, _
    > DataOption1:=xlSortNormal
    > End Sub
    >
    >
    > --
    > mudraker
    > ------------------------------------------------------------------------
    > mudraker's Profile:
    > http://www.excelforum.com/member.php...fo&userid=2473
    > View this thread: http://www.excelforum.com/showthread...hreadid=535627
    >




  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    WylieDude

    The following is a Single line command. -I did not notice that it had been word wrapped when I originally posted


    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Move Boolean) back to the previous line after Cancel As

  5. #5
    Registered User
    Join Date
    12-14-2016
    Location
    OXFORD
    MS-Off Ver
    2016
    Posts
    1

    Re: auto sort automatically on save

    Mudraker,
    I am a complete beginner at using a macro in excel but found this nice an easy to understand, thanks!

    I am trying to sort my data first by column A (Country), then B(City/Town), then C(Place), then D(Link to URL) as you can see I the attachment.
    Can you alter the macro you originally posted to incoperate this? It would be great if it did it for the entirety of each column as i plan to add serveral rows
    Attached Files Attached Files

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: auto sort automatically on save

    anbs welcome to the forum

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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