+ Reply to Thread
Results 1 to 10 of 10

VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

  1. #1
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hi all,

    I have the macro below which opens csv files stored in a user selected folder and processes them changing the date format in column D from DD/MM/YYYY to text stored as YYYY-MM-DD.

    For most of the dates the code works without issue, but for some (those with a month <12 possibly) it transposes the MM and DD incorrectly.

    I understand that when opening the CSV's in excel it automatically converts the dates to DD/MM/YYYY, so I'm actually opening in wordpad which displays as YYYY-MM-DD, with only a portion being incorrect.

    I've attached a couple of sample files (pre and post conversion). Any help in solving this would be appreciated.

    Cheers

    David




    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hello Dave
    I import a csv file every day & ran into the same problem you have. The file I import has a "dd/mm/yyyy hh:mm:ss" format day/date column that loses it's proper formatting & goes AWOl to that other format.
    I copy the cell contents into a variable formatted as "dd/mm/yyyy hh:mm:ss", then use the Right(blah blah) function to copy the time portion into the adjacent cell, then clear the contents of the source cell, copy the date portion back into the source cell using the DateSerial(year,month,day) function & finally format the source cell to "dd/mm/yyyy"
    It works 100% every time
    I've attached a workbook with the code for you to experiment with
    Cheers
    Phil
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Thanks Phil,

    On investigation I've found the following code from which seems to work for my situation, however I can't seem to incorporate it into my vba:

    This works:
    Please Login or Register  to view this content.
    Would anyone have anu ideas as to why the macro seem to process the files but not save them?

    Please Login or Register  to view this content.
    Thanks

    David

  4. #4
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hi David. I looked at your code & finally (I think) worked out what it's doing. I don't know what the x=x+1 is for, I'm guessing it's for counting how many dates were formatted.
    I merged some of your code with some of my code & I think it's doing what you want, including saving it.
    Copy the code below into a new module & experiment. It uses your GetFolder function
    Good Luck
    Phil
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hi Phil,

    Thanks for modifying the code. I've run it through and it certainly seems to modify and save the first file sucessfully.

    However it doesn't cycle through all of the CSV files in the folder, of which there are 20. It stops after the 1st file.

    The original code would cycle through and display a message advising that X number of files have been processed.

    Thanks

    David

  6. #6
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hi Phil,

    I'll try posting another message and see if anyone can help further.

    Thanks for your assistance so far.

    Cheers

    David

  7. #7
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hello David. I can't access the site via my computer for some reason,so I'm fighting with my phone.I'll have a look as soon as someone's fixed the reason I can't get on the site, unless someone else helps you first.Its only this site that's giving grief.

  8. #8
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Hi Dave
    I got my site access back
    This code will loop through all the .csv files in a folder you select via the browse box. It uses your "GetFolder" function
    Please Login or Register  to view this content.
    If you go to the same folder every time you can use this code. Change the "FolderPath" path to suit
    It doesn't need the "GetFolder" function
    Please Login or Register  to view this content.
    Fingers crossed one of them will work for you
    Cheers
    Phil

  9. #9
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    Phil,

    These are both fantastic. Very much appreciated

    Regards

    David

  10. #10
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA incorrectly changing some dates from DD/MM/YYYY to YYYY-DD-MM

    No worries Dave
    Cheers
    Phil

+ 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. [SOLVED] Converting dates from dd/mm/yyyy to mm/dd/yyyy
    By treeantz in forum Excel General
    Replies: 6
    Last Post: 01-04-2013, 03:34 AM
  2. Replies: 0
    Last Post: 10-14-2012, 05:48 PM
  3. Copy+Paste macro dd/mm/yyyy to dd/mm/yyyy and NOT dd/mm/yyyy to mm/dd/yyyy
    By uimhirADo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2012, 03:45 AM
  4. Macro to convert dates format from yyyy mm dd to yyyy-mm-dd
    By Gators in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 02-26-2009, 02:23 PM
  5. Changing date entries from dd/mm/yyyy to mm/dd/yyyy
    By dariusjr in forum Excel General
    Replies: 2
    Last Post: 08-13-2008, 11:33 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