+ Reply to Thread
Results 1 to 8 of 8

Date changes went being pasted

  1. #1
    Forum Contributor
    Join Date
    01-21-2017
    Location
    England
    MS-Off Ver
    2007
    Posts
    409

    Date changes went being pasted

    Hi I have the following code that pastes data from a csv file to the excel spreadsheet. However I have noticed that on the original csv file the date's in one of the column appears as dd/mm/yy which is correct but when copied across to sheets(4) they sometimes appear the right way and other times mm/dd/yy.

    Please Login or Register  to view this content.
    Any help would be appreciated.

    Cheers

    Dave

  2. #2
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Date changes went being pasted

    If I remember correctly, csv files assume the use of American date settings (mm/dd/yyyy) by default. So what's probably happening is that when the date can be an American date (for example 05/08/2018) then it will be assumed to be that date (in that example, 8th May 2018, even if that date was meant to be 5th August 2018). If the date cannot be an American date (for example 19/08/2018) then it will be assumed to be the non-American date.

    When saving a file with international dates into csv, it's possible to force csv to use international, not American, settings, by using Local:=True. For example:
    Please Login or Register  to view this content.

    The bad news, however, is that I don't know how to insert this into your code - or if it would even work - my VBA skills just aren't that good. But maybe it'll give you, or someone else on here, a starting point?
    Good luck.
    Regards,
    Aardigspook

    I recently started a new job so am a bit busy and may not reply quickly. Sorry - it's not personal - I will reply eventually.
    If your problem is solved, please go to 'Thread Tools' above your first post and 'Mark this Thread as Solved'.
    If you use commas as your decimal separator (1,23 instead of 1.23) then please replace commas with semi-colons in your formulae.
    You don't need to give me rep if I helped, but a thank-you is nice.

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Date changes went being pasted

    Aardigspook is indeed correct. When Excel imports from a CSV if it can be an American date it formats it as such, and if it cannot then it formats it as text. If you resize the column to be wider than necessary and remove any left/right/centre justification you'll see the American dates aligned to the right and the others to the left.

    I had exactly this issue at work just before Xmas. My solution was to have the VBA loop through the affected column, row by row, and if it contained a date it would switch the day and month around in a helper column and if not it would use DATEVALUE to convert the text date into an actual date in the helper column. When done, all the dates are in DD/MM format and the helper column can be copied and stamped back over the original column.

    At least that's how I think I did it. I don't have access to the file right now.

    Very interested to hear other solutions on this! :D

    BSB
    Last edited by BadlySpelledBuoy; 01-05-2019 at 03:13 PM.

  4. #4
    Forum Contributor
    Join Date
    01-21-2017
    Location
    England
    MS-Off Ver
    2007
    Posts
    409

    Re: Date changes went being pasted

    Many thanks for the replies, it would be great if someone could provide an easy solution. I’d rather not use a helper column but if that’s not possible then I’ll go for it. Unfortunately I have no way of modifying the csv file as it’s created by another program and then I use the above code to import it.

  5. #5
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Date changes went being pasted

    I'd rather not have used a helper column either, but I was pushed for time to spend thinking it through.

    The helper column is temporary and by the time the code finishes running it's been deleted so nobody is any the wiser.

    BSB

  6. #6
    Forum Contributor
    Join Date
    01-21-2017
    Location
    England
    MS-Off Ver
    2007
    Posts
    409

    Re: Date changes went being pasted

    Hi doesn't look like anyone's come up with a solution. If you could let me know what the formulae is for the helper column I would be most grateful.

    Cheers

    Dave

  7. #7
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Date changes went being pasted

    This formula should work in a helper column.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    If A2 is being read by Excel as a date, it will switch the day and month (for example 11/05/19 will become 05/11/19).
    If the 'date' is text, it will extract the relevant parts of the text and make them a date.
    It assumes that all the dates, whether being read as dates or text, are in the American month/day/year format.
    It also assumes that the text-dates have 4-digit years like 9/28/2019 (because that's how a csv-import presented my test file dates to me). If you're getting 2-digit years in your text-dates (e.g. 9/28/19) then change the red 4 to a 2.

    If the formula doesn't work for you, get back to us with what incorrect results you're getting.

    Also, I'm afraid I can't help with doing this in VBA - it's not my strong point.

    Hope that helps a bit.

  8. #8
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Date changes went being pasted

    Good work Aardigspook. In a roundabout way that's the same formula I used in my helper column (slight difference, but nothing dramatic).

    Throughstream - If you'd like help making this work via VBA (helper column being added, utilised then thrown away so nobody is any the wiser) then I'll be back in the office and will have access to the code I've already written tomorrow morning so give us a shout.

    BSB

+ 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. Replies: 15
    Last Post: 10-21-2018, 01:54 AM
  2. [SOLVED] Add Date to end of newly pasted cells
    By mcanfield in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2016, 03:23 PM
  3. date pasted as 1/2/1900 in excel instead of 1/1/1900
    By ekkslatha in forum Excel General
    Replies: 4
    Last Post: 10-25-2014, 08:34 PM
  4. Replies: 4
    Last Post: 09-19-2014, 11:26 AM
  5. [SOLVED] VBA to insert date when text is pasted into adjacent cells
    By Trax in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-07-2012, 04:01 AM
  6. [SOLVED] Getting the MIN of a pasted date value
    By shinblue in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-18-2012, 06:46 PM
  7. Replies: 3
    Last Post: 08-11-2006, 09:10 AM

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