+ Reply to Thread
Results 1 to 7 of 7

Help required: VBA ignoring regional date settings - using US format as default.

  1. #1
    Registered User
    Join Date
    10-26-2012
    Location
    Chelmsford, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Help required: VBA ignoring regional date settings - using US format as default.

    Hi all,

    I am importing a series of dates from a .csv file which look like "DD.MM.YYYY" (this is an export from a piece of software I do not have any power over and so is not negotiable) and I require the date format "DD/MM/YYYY". I do not know what format the dates are imported in.

    To convert from one to the other I have used:

    Please Login or Register  to view this content.
    Which came from the 'Record Macro' function. However, this returns correctly formatted dates if and only if the "DD" value is less than or equal to 12. I have read a few forum posts and I understand that this is because VBA defaults to US date settings - if I perform these exact changes manually I obtain the correctly formatted dates for all entries.

    I would very much appreciate your help with this issue. Please let me know if I can provide any additional information.

    Thanks in advance,

    Josh.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Josh

    If you don't know what format the dates are in how can you convert them?

    By the way, have you tried dealing with the dates when importing?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-26-2012
    Location
    Chelmsford, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Help required: VBA ignoring regional date settings - using US format as default.

    Hi Norie, and thanks for your reply.

    I am converting the dates as shown in the code above. When imported, the dates look like "01.01.2013" and "30.01.2013" but afterwards they look like "01/01/2013" (right aligned, works as a date in the rest of my spreadsheet) and "30/01/2013" (left aligned, does not work in the rest of my spreadsheet) respectively. Would a screen grab help?

    In what way could I 'deal' with the dates when importing?

    Regards,

    Josh.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Help required: VBA ignoring regional date settings - using US format as default.

    Josh

    How are you getting the date into Excel from the CSV?

    The reason I ask is because if you were importing via the text wizard you could use that to deal with the dates.

    You might still be able to deal with them all in one go, with no code, just select the column of dates, goto Data>Text to columns and on the 3rd step choose YMD for the Column data format.

    Of course if you want code.
    Please Login or Register  to view this content.
    Note, this is for the whole column and assumes no header, it would probably be an idea to adjust it so it's limited to the range with dates.

  5. #5
    Registered User
    Join Date
    10-26-2012
    Location
    Chelmsford, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Help required: VBA ignoring regional date settings - using US format as default.

    Hi Norie,

    I am importing the csv using VBA. The segments of code I have provided are actually part of a larger 'update the whole spreadsheet' macro.

    Please Login or Register  to view this content.
    Which, again, I got from the record macro wizard.

    Thank you for writing out some code for me - I will try and tweak it to my spreadsheet and see if it works.

    Regards,

    Josh.

  6. #6
    Registered User
    Join Date
    10-26-2012
    Location
    Chelmsford, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Help required: VBA ignoring regional date settings - using US format as default.

    Hi Norie,

    The code you provided worked once tweaked. Thank you very much for your help.

    If you have the time, could you explain what was different about our approaches, so that I can avoid similar problems in the future?

    Best wishes,

    Josh.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Help required: VBA ignoring regional date settings - using US format as default.

    Josh

    When you open a text file in Excel you usually get a Text Import wizard.

    This allows you to tell Excel the file is delimited or fixed width and you then go through various other steps.

    On one of these steps (the 3rd) you can specify the data format for each column in the file.

    One of the format options is Date, which itself has various date format options YMD, MDY, YDM etc

    If you have a column with dates you should pick the Date option and pick the format that corresponds
    to the data you are importing.

    For example you had dates ormatted as DD.MM.YYYY so you would pick DMY.

    After you've finished with the wizard you should hopefully have the data formatted as you want.

    Since your data had already been imported I used Data>Text to columns... which has much the same
    options as the Text Import Wizard.

    Hope some of that makes some sort of sense.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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