Consulting

Results 1 to 11 of 11

Thread: stupid dates!!!

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    23
    Location

    stupid dates!!!

    I am reading data in from a number of excel files, and putting it all in one file, and am having problems with dates. The format in the original files is dd/mm/yyyy hh:mm, the format in the destination file is the same, but when I read a cell with a date, eg 01/11/2008, it is getting read as 11/01/2008 before I even do anything with it. ie, I read in the value, and when i do debug,print, or msgbox, the value at runtime is 11/01/2008. This is really confusing me and there doesnt seem to be any way around it. format() doesnt help. Any ideas on whats going on here?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This is a common issue. How are reading the data?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2010
    Posts
    23
    Location
    [VBA]
    Set r = WB.Sheets(1).Range("D1", temp)
    '
    '
    '
    for each c in r
    '
    c.Offset(0, 1).Value)
    '
    '
    [/VBA]

    im setting a range from first to last cell in sheet, then reading the value in as an offset to the original range.

    I have found that if I remove all formatting ( cell styles-> normal) then it reads in as a double(?) and from there I can convert it in vba to a date, trying to work out how to remove formatting from all the cells in the range now

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. In the destination file, could you format the date cells as dd/mm/yyyy? It sounds like it a matter of how the date is being displayed.

    2. Try using the .Text instead of the .Value. That should bering it over as displayed, and then you can manipulate it if you have to

    Paul

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by neeeel
    [VBA]
    Set r = WB.Sheets(1).Range("D1", temp)
    '
    '
    '
    for each c in r
    '
    c.Offset(0, 1).Value)
    '
    '
    [/VBA]

    im setting a range from first to last cell in sheet, then reading the value in as an offset to the original range.

    I have found that if I remove all formatting ( cell styles-> normal) then it reads in as a double(?) and from there I can convert it in vba to a date, trying to work out how to remove formatting from all the cells in the range now
    Just cast it as a date.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Oct 2010
    Posts
    23
    Location
    ye ive tried that, but for some reason it casts as the wrong date, ie, mm/dd/yyyy instead of dd/mm/yyyyy

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Casting a date doesn't format it, it just ensures it is stored as a date. You can format as you want.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Oct 2010
    Posts
    23
    Location
    formatting doesnt work, for some reason, the date is getting copied incorrectly, so no matter how i format it, a date stored as 01/06/2010 ( where it should be 06/01/2010) is still going to give me the 1st June 2010

  9. #9
    VBAX Regular
    Joined
    Oct 2010
    Posts
    23
    Location
    to quote from excel help,

    "CDate recognizes date formats according to the locale setting of your system. The correct order of day, month, and year may not be determined if it is provided in a format other than one of the recognized date settings"

    the dates in the original sheets are obviously stored in some weird format. Using day() and month() still returns the wrong date on the raw data, ie day 1 and month 6, instead of day 6 month 1. I really cant see a way around this, can anyone help?

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    In the source/original file, are you sure that Excel is reading it as a date and not text?

  11. #11
    VBAX Regular
    Joined
    Oct 2010
    Posts
    23
    Location
    I think i have solved this, the original files are CSV format, by saving the files as excel worksheets, I think I get the correct dates. Just converting all the files now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •