+ Reply to Thread
Results 1 to 13 of 13

Do while loop and date sorting

  1. #1
    Registered User
    Join Date
    12-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Do while loop and date sorting

    I've a macro to read the file names in a folder and move the file names to a cell. 1 cell per file name. File names has dates but the single digit dates don't have leading zeros so they are not sorted correctly and I've to insert the leading zeros for this to work correctly. Is there a way to make excel sort the file name correctly.

    File names appear as below in explorer:
    abcd 10-8-10
    abcd 10-9-10
    abcd 10-10-10

    but after running the macro they appear as below in the cell:
    abcd 10-10-10
    abcd 10-8-10
    abcd 10-9-10

    Macro:
    FNames = Dir("*abcd*.xls")
    Do While FNames <> ""
    ....
    .....
    ....
    loop
    Last edited by excel_sunsan; 12-19-2010 at 11:49 PM. Reason: Marked as Solved

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Do while loop and date sorting

    Hi excel_sunsan
    welcome to the forum,
    you could modify this ...
    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    12-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Do while loop and date sorting

    Thanks Pike for a quick response.

    However, I'm still getting the result as
    abcd 10-10-10.xls
    abcd 10-8-10.xls
    abcd 10-9-10.xls

    However, I need it as follows:
    abcd 10-8-10.xls
    abcd 10-9-10.xls
    abcd 10-10-10.xls

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Do while loop and date sorting

    Hi excel_sunsan

    One way would be add the values to a collection the do a quick sort
    Please Login or Register  to view this content.
    Last edited by pike; 12-19-2010 at 04:27 AM.

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Do while loop and date sorting

    Hi excel_sunsan

    It finaly clicked on what you meant... this will sort by the date
    it would be easier just to filter the cells
    Please Login or Register  to view this content.
    Last edited by pike; 12-19-2010 at 05:00 AM. Reason: remove debug.print

  6. #6
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Do while loop and date sorting

    As a tip, its better to name files with dates YYYY-MM-DD Name.XLS that way explorer can sort them in order!
    Regards
    Darren

    Update 12-Nov-2010 Still job hunting!

    If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

    Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!


  7. #7
    Registered User
    Join Date
    12-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Do while loop and date sorting

    pike - Thanks for the code. Looks like its never entering the IF..ENDIF. But thats OK, I think we will change the filename format so it will sort as in Explorer.

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Do while loop and date sorting

    hi excel_sunsan
    The "abcd" needs to be changed
    Please Login or Register  to view this content.
    try the code from the post as I did tweak it

  9. #9
    Registered User
    Join Date
    12-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Do while loop and date sorting

    I meant its not entering the below IF statement.
    Please Login or Register  to view this content.
    I tried both your above codes and I'm still not getting it like:
    abcd 10-8-10.xls
    abcd 10-9-10.xls
    abcd 10-10-10.xls

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Do while loop and date sorting

    arr. are the date formated day month year?

  11. #11
    Registered User
    Join Date
    12-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Do while loop and date sorting

    No, they are month day year.

  12. #12
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Do while loop and date sorting

    hi excel_sunsan
    change the "Short Date" to 0 as per below this will make the date a serial number


    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    12-18-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Do while loop and date sorting

    pike.....Thank You! Thank You! Thank You!

    It worked as I was expecting it.

+ 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