+ Reply to Thread
Results 1 to 13 of 13

VBA for Custom Autosort

  1. #1
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    VBA for Custom Autosort

    Hi All,

    I was hoping some one could help me out with a Macro or some VBA to solve this problem, here are the criteria:

    1: Need to automatically sort row data with a common date by the value in Column G in ascending order (A-Z).

    2: Row data is updated and added to everyday

    3: Sort needs to be performed on Save (BeforeSave Event)

    As an example, when I record a Macro to sort the 2-Oct row data in the attached workbook, I get this:

    Please Login or Register  to view this content.

    Obviously when I would run this macro it would only select and sort the data in the range i've manually selected, however I need the worksheet to perform that action automatically when the user saves the workbook. The common group it has to sort by is whatever date is inputed by the user in the date column, which will be added to everyday, sometime with multiple dates per day.

    Ive attached a book with a before and after example.
    Thanks for your help!
    Attached Files Attached Files
    Last edited by Pierce Quality; 11-06-2013 at 08:40 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA for Custom Autosort

    Hello Pierce Quality,

    Can we assume the date used for the sort will always be today's date?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for Custom Autosort

    Quote Originally Posted by Leith Ross View Post
    Hello Pierce Quality,

    Can we assume the date used for the sort will always be today's date?
    Hello Leith!

    The date will not be the current date, but will always be grouped as the same date, entered by the user, as a value in the Date column.

    Thanks!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA for Custom Autosort

    Hello Pierce Quality,

    Will the user enter the new date group below the old data or can the data be inserted anywhere the user likes?

  5. #5
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for Custom Autosort

    Quote Originally Posted by Leith Ross View Post
    Hello Pierce Quality,

    Will the user enter the new date group below the old data or can the data be inserted anywhere the user likes?
    Hello Leith,

    It will be inserted below the data as time progresses, always ascending.
    Thanks!

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA for Custom Autosort

    Hello Pierce Quality,

    So, the macro also needs to save the last row number when the workbook is saved to know where the new data will start next time, yes?

  7. #7
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for Custom Autosort

    Quote Originally Posted by Leith Ross View Post
    Hello Pierce Quality,

    So, the macro also needs to save the last row number when the workbook is saved to know where the new data will start next time, yes?
    Hello Leith,

    Yes, the sheet will be updated my multiple users several times a day.
    Thanks!

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA for Custom Autosort

    Hello Pierce Quality,

    Okay, I think this should work for you. The macro will create a log file, in the same directory as the workbook, that contains the last row on the worksheet "BeforeData" whenever the users does a Save or SaveAs. The range to be sorted is defined from the old last row + 1 to the current last row. The data is sorted first by Date then by Area.

    The attached workbook contains the macro code shown below.

    [b[Module1 Macro Code[/b]
    Please Login or Register  to view this content.
    ThisWorkbook BeforeSave Event Macro Code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for Custom Autosort

    Quote Originally Posted by Leith Ross View Post
    Hello Pierce Quality,

    Okay, I think this should work for you. The macro will create a log file, in the same directory as the workbook, that contains the last row on the worksheet "BeforeData" whenever the users does a Save or SaveAs. The range to be sorted is defined from the old last row + 1 to the current last row. The data is sorted first by Date then by Area.
    Hello Leith,

    I'm afraid that direction wont quite work for our situation with contiginous data, but is very close. In speaking with some of the users, we wont need to last row data to be saved anymore and we are going to run it as a macro instead of a before save event on a single book to pilot the change.

    Here's what I ended up coming up with:

    Please Login or Register  to view this content.
    This seems to work well for a basic application so far, I also set the end selection to be on the first open cell in column A after the data, (you would need to add a header in A1 in the example book for this to work).
    Do you have any insights on how I could make this code better? Eventually this will be sorting tens of thousands of rows of data so I would like to work out the kinks as early as I can.

    Thanks!

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA for Custom Autosort

    Hi Pierce Quality,
    maybe so
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for Custom Autosort

    Quote Originally Posted by nilem View Post
    Hi Pierce Quality,
    maybe so
    Please Login or Register  to view this content.
    Hello nilem,

    I ran the code as this

    Please Login or Register  to view this content.
    on about 8000 rows of data vs the other auto sort on the same amount of data and it appears that yours is slightly faster, so I think it will be the way to go. Is there a faster way to select the first open cell in column A after the data?
    Thanks!

  12. #12
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA for Custom Autosort

    Hi Pierce Quality,
    try it (not faster, it just prettier )
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for Custom Autosort

    Quote Originally Posted by nilem View Post
    Hi Pierce Quality,
    try it (not faster, it just prettier )
    Please Login or Register  to view this content.
    Thanks nilem, prettier is better for sure!

    Ill be closing this thread as solved now, special thanks to Leith Ross and nimlem for all the help!

    Thanks again!

+ 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. Autosort
    By giacc in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-14-2011, 06:12 PM
  2. how to autosort
    By jjenred5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2011, 07:03 PM
  3. Autosort Help
    By JoeVember in forum Excel General
    Replies: 0
    Last Post: 04-30-2009, 06:28 PM
  4. autosort
    By pete in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-25-2006, 06:10 PM
  5. [SOLVED] AutoSort in VBA
    By tamato43 in forum Excel General
    Replies: 1
    Last Post: 03-30-2005, 04:06 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