+ Reply to Thread
Results 1 to 7 of 7

VBA to Fix date format - alter string programmatically

  1. #1
    Registered User
    Join Date
    04-08-2014
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    29

    VBA to Fix date format - alter string programmatically

    Hi, I have a date field being exported from an asp OMS, This field is sometimes populated as YYYY-MM-DD. When the year is shown first the format I end up with on export in excel looks like this 20/07/8/6/ instead of 8/6/2007

    I was looking for some vba code to re-format this definitive pattern so it would be formatted MM-DD-YYYY, to be run on a button click function in my existing macro project.

    It needs to be recognized as a TRUE date format because I pull the YEAR out from a helper column. (and yes it'd be GREAT to fix the date format in the OMS befre export, but that will not happen any time soon and i have no control over that change, only this export part!)

    Any suggestions how this can be done? THANKS!

    using excel 2007
    Last edited by EXLwiz; 06-23-2014 at 07:28 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,336

    Re: VBA to Fix date format - alter string programmatically

    Maybe:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    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 to Fix date format - alter string programmatically

    Hello EXLwiz,

    You say "This field is sometimes populated as YYYY-MM-DD". Does that mean there are different date formats besides this one being impported?
    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!)

  4. #4
    Registered User
    Join Date
    04-08-2014
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA to Fix date format - alter string programmatically

    Thanks for the quick reply..

    Yes infact this works to fix the date format. I had to modify the code to match my column which is in col AA (AA2:AA1000 - AA1 is a header row). but then i tried a few scenarios and it broke down. runtime error 9 subscript out of range. i debugged and found the cell value passed into the array was already formatted correctly as mm/dd/yyyy

    It also needs to check for:

    whether the cell is empty (some cells will not be populated at all),
    if the format is already as desired,
    if there's a single digit format used like m/d instead of mm/dd

    sample data i tested with (10 rows)

    (header cell)
    20/07/8/26/
    07/07/2007

    20/12/4/5

    20/07/8/6/
    19/87/12/4
    08/06/2007
    20/12/4/18

  5. #5
    Registered User
    Join Date
    04-08-2014
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA to Fix date format - alter string programmatically

    Hi @Leith Ross.. yes, i just posted another comment explaining more details.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,336

    Re: VBA to Fix date format - alter string programmatically

    1. The code catered for the scenario you described
    2. You didn't say which column your data was in
    3. You didn't say your data had header(s)
    4. You didn't say your data had blank cells in it
    5. The code caters for single digit month and day using Split
    6. The code won't cope if any of the elements are missing
    7. The output is formatted as mm/dd/yyyy and it is a true date so that's immaterial.

    Don't expect us all to have second sight. You need to describe the data and the issues you have to fix.

    Sorry you had to put some effort into fixing your data.

  7. #7
    Registered User
    Join Date
    04-08-2014
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA to Fix date format - alter string programmatically

    Quote Originally Posted by TMS View Post
    1. The code catered for the scenario you described
    2. You didn't say which column your data was in
    3. You didn't say your data had header(s)
    4. You didn't say your data had blank cells in it
    5. The code caters for single digit month and day using Split
    6. The code won't cope if any of the elements are missing
    7. The output is formatted as mm/dd/yyyy and it is a true date so that's immaterial.

    Don't expect us all to have second sight. You need to describe the data and the issues you have to fix.
    @TMS - i never meant to offend you in the least and was cetainly not trying to sound critical, i was simply factual and pressed for time when i posted earlier. i'm not that familiar with vba, i do know .NET and am very handy with in-cell formulas, but .. apples and oranges are both fruit right??

    so i threw a quick post out hoping i'd get just the piece i needed that changes the format and could manage the rest, but honestly I dont have the time or knowledge enough in vba with arrays (demons, as i call them) to do it myself. I didn't say i changed your code to meet my column name to be critical, just as a matter fact.. it was no biggie (i thought) which col it actually is in, since i do know enough vba to know how to change the columns and adjust for the headercell

    to be fair, i did say SOMETIMES the date is goofy format, i thought that would have been an indicator to check if it needs a format change or not.

    anyway, i figure i need an IF stmt added to check if the cell is NULL value at this point, and skip over that iteration in the loop if it is, but wasnt sure how to achieve that on the fly because i barely get by with arrays in .NET (yeah arrays+me=dont get along well)

    so i posted more details to clarify.. sorry, i should've taken the time the first attempt.

    Many huge thanks for your assistance, i sincerely didn't mean to sound unappreciative!
    Last edited by EXLwiz; 06-23-2014 at 10:38 PM.

+ 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. Need Formula/VBA Code to convert date in String Format to Normal Date format
    By Karthik Sen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2012, 04:54 AM
  2. [SOLVED] Excel date format convert to string format
    By man in forum Excel General
    Replies: 1
    Last Post: 08-17-2006, 05:05 AM
  3. [SOLVED] Date format in string
    By Adam Thwaites in forum Excel General
    Replies: 3
    Last Post: 05-10-2006, 11:20 AM
  4. Can't programmatically set a large array string to a range.
    By Lu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-03-2006, 10:00 PM
  5. [SOLVED] Programmatically alter the Project Explorer
    By R Avery in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2005, 09: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