+ Reply to Thread
Results 1 to 10 of 10

Modify Macro to Sort Properly

  1. #1
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Modify Macro to Sort Properly

    Hi Everyone!

    I found the following Macro through a search to help a co-worker sort their excel workbook. Their problem is that they have a massive amount of sheets, and they need to be in order by date.

    When I went to run the macro on their sheet this morning we discovered another problem: The way the dates were written.

    Right now the dates on each sheet are written as (this is just for illustration, they days won't match up):

    Wednesday 1-21-15
    Friday 1-1-15
    Wednesday 1-2-15
    Wednesday 1-12-15


    Running the sort, because the days are written first, the sort will group them by day then by sequential number. The list looks like:


    Friday 1-1-15
    Wednesday 1-12-15
    Wednesday 1-2-15
    Wednesday 1-21-15

    Is there any way to modify the following macro to make these sheets, as they are currently named, go in order by date?

    Or is there another macro that will do the same trick?

    Thank you everyone!

    Please Login or Register  to view this content.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Modify Macro to Sort Properly

    Add this UDF :
    Please Login or Register  to view this content.
    And change these lines :
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Modify Macro to Sort Properly

    Change this section
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Modify Macro to Sort Properly

    Hey guys thanks for the fast response!

    Karedog,

    I added the UDF into a second module, and changed the lines you indicated. Marco ran without issue but nothing happened.

    Mike,

    All three of your "LT=..." lines appear red in the VBA editor. When I delete then add back the end parentheses and hit enter, I get a "Compile error: Expected: )"

    I tried running the macro regardless and I got: "Compile error: ByRef argument type mismatch" and the code that was highlighted was the "N" in the line that read "If LT(N, M) Xor....."

    Any thoughts? Or could either of you make a test file to ensure I'm not messing up some transfer?

    Let me know what you need form me and thanks for your help so far!

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

    Re: Modify Macro to Sort Properly

    Just remove the first opening paren on those three lines.

    As an alternative, how about a macro that starts by renaming the sheets rationally -- yyyy-mm-dd. Then the sort is simple.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Modify Macro to Sort Properly

    Can you upload your sample file ?

    You can test with mine here :
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-10-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    4

    Re: Modify Macro to Sort Properly

    An easy way is to make it so that the dates are in reverse - 20151201 = 2015, 12 (December), 01 (1st). That way when things are sorted by name, they are sorted by date (i.e. 20151201 comes after 20120304). Could you write a macro to basically obtain the file name dates in a column, do text to columns to split out the year, month and day, then use the "Date" formula to actually write them all into a recognisable date format - re-format those cells to yyyymmdd, then change the names of the worksheets you have so that the dates in the worksheet name are yyyymmdd....
    Then in your "Sorting" macro, all you have to do is sort by name and they'll be date arranged....

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,560

    Re: Modify Macro to Sort Properly

    Quote Originally Posted by liquidmettle View Post
    Or is there another macro that will do the same trick?
    Try
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Modify Macro to Sort Properly

    Hello All,

    Karedog your example file was perfect. Turns out I made the novice error of putting the Option Explicit Function in its own module.

    Thanks to everyone who came to my rescue!

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Modify Macro to Sort Properly

    Hi liquidmettle, you are welcome, and thanks for the reputation points.


    Regards

+ 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. Sort not working properly
    By bradleyandrewdavis in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-02-2015, 02:01 PM
  2. Excel won't sort my values properly
    By KoshX87 in forum Excel General
    Replies: 2
    Last Post: 05-08-2015, 11:00 PM
  3. Won't permanently sort properly
    By GigaFluxx in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-14-2015, 11:32 AM
  4. [SOLVED] One cell won't sort properly in one column
    By joebell in forum Excel General
    Replies: 2
    Last Post: 11-10-2013, 02:13 AM
  5. My numbers do not sort properly
    By MiriBella in forum Excel General
    Replies: 7
    Last Post: 06-20-2012, 06:31 PM
  6. Get macro to sort dates properly
    By DamienC in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2008, 02:53 AM
  7. Modify Auto Sort Macro to delete Duplicate entries
    By dj_mix in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2006, 04:17 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