+ Reply to Thread
Results 1 to 4 of 4

Macro to open CSV file, and reformat date field into text and save as YYYY-MM-DD

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

    Macro to open CSV file, and reformat date field into text and save as YYYY-MM-DD

    Hi all,

    I have a macro which clycles through a folder of CSV files making changes (normally deleting rows of columns).

    I'd like to adopt it instead to reformatting in place (if possible), or copying/paste special with a formula similiar to =TEXT(D2,"YYYY-MM-DD) which works outside of VBA.

    The dates to be reformatted will always be in column D (starting at D2), however the number of rows will vary.

    From past experience I know that CSV files don't interpret dates as text and after the change is make should it be re-opened it will display as per my regional settings, but the next person to view will be doing so via a text editor and the change should be sufficient for the formatting to stick.

    The following code is about as far as I've managed to get, could anyone add anything to it?


    Please Login or Register  to view this content.



    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to open CSV file, and reformat date field into text and save as YYYY-MM-DD

    You couldn't put a formula in the same cell as the date value. So if column D has the dates, you couldn't have the TEXT formula in column D as well.

    If you change the display format of the dates, then resave the CSV file, it will use the displayed date format as the saved text.

    Use this to set the date format of all dates in column D.

    Range("D:D").NumberFormat = "YYYY-MM-DD"
    Last edited by AlphaFrog; 04-09-2014 at 09:30 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

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

    Re: Macro to open CSV file, and reformat date field into text and save as YYYY-MM-DD

    Hi Alphafrog,

    I've tried the code suggested but it seems to mix the dates and months around, or not touch them at all.

    The 02/05/2014 becomes 2014-02-05

    The 09/05/2014 becomes 2014-09-05

    The 16/05/2014 remains the same

    The 23/05/2014 remains the same.

    Any idea why?

    T

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to open CSV file, and reformat date field into text and save as YYYY-MM-DD

    Quote Originally Posted by DaveBre View Post
    Hi Alphafrog,

    I've tried the code suggested but it seems to mix the dates and months around, or not touch them at all.

    The 02/05/2014 becomes 2014-02-05

    The 09/05/2014 becomes 2014-09-05

    The 16/05/2014 remains the same

    The 23/05/2014 remains the same.

    Any idea why?

    T
    I have a notion.

    The original CSV files have dates in the format of dd/mm/yyyy.

    Your computer's regional setting is currently English(United States) and not English(Australia)

    When you open the CSV file, Excel using English(United States) will wrongly convert some of the dates as mm/dd/yyyy when the day as less than or equal to 12 in the CSV file. Those days can wrongly be converted to months because the day is 12 or less. When the day is greater than 12, the text-date remains as text and not converted to a serial-date. The text-dates are not affected by the .NumberFormat property. Only the wrongly converted serial dates would be formatted.


    Check your computer's regional setting for English(Australia). Change it if necessary and reopen the CSV file. That should fix it if my assumption is correct.
    Last edited by AlphaFrog; 04-09-2014 at 11:01 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. [SOLVED] Macro to Save As a file with a date in dd-monthname-yyyy
    By witcomm in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-21-2013, 02:20 AM
  2. Macro to open csv file converts dd/mm/yyyy to mm/dd/yyyy
    By AlexRoberts in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2012, 08:40 AM
  3. Macro that open, read, modify and save a text file
    By cabinder in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-15-2012, 11:03 AM
  4. Macro to open URL then save page as a text file
    By jasocke2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2009, 10:06 AM
  5. [SOLVED] opening excel file -> date format problem: DD/MM/YYYY vs MM/DD/YYYY
    By yung in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-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