Consulting

Results 1 to 10 of 10

Thread: Solved: COPYING DATES FROM WORD TO EXCEL

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    28
    Location

    Solved: COPYING DATES FROM WORD TO EXCEL

    Hi,

    I have set up a macro that copies all of a WORD document containing a table of data to EXCEL and have been having a problem copying dates that I was hoping someone could help with.

    The routine that works correctly when carrying out the process manually is to copy all of the WORD document to the clipboard and then paste this as text using Paste Special in EXCEL.

    The problem when programming VBA to do this is that the dates are not copied correctly. Examples of the problems being as follows:

    01/10/08 (i.e. 1 October 2008) converted to 10/01/08
    16/10/08 (i.e. 16 October 2008) coverted to string "16/10/08" not a value

    I realise this is something to do with the dates copied being treated as US dates (i.e. mm/dd/yy) instead of UK dates (dd/mm/yy) but can't understand why the copy routine works manually and not in VBA ?

    Is there a line that can be added to VBA to stop this problem ?

    Thanks for any advice.

    Goggs75

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Goggs, I've moved this to the Excel help forum where I think you will stand a better chance of someone taking a look at it with you.

    Can you format the column or cells where the date is transferred into Excel before you paste the data or is it a new Excel workbook each time?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi,

    Always best to post a small example or at least the code.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  4. #4
    VBAX Regular
    Joined
    Oct 2007
    Posts
    28
    Location
    Hi Lucas/Rbrhodes

    I have tried formatting the cells prior to copying from the word document and this does not solve the problem.

    The line of code not working is as follows:

    Activesheet.PasteSpecial Format:="Text"

    I have found the following code does work as a one line macro but not when building it into the intended macro:

    Application.SendKeys ("%ES{END}{UP}~")

    (I.E. EDIT PASTESPECIAL TEXT ENTER)

    An example of the problem being as follows:

    A date of 3/10/07 changes to 10/03/07 whereas a date of 15/10/07 remains as 15/10/07 but is left aligned rather than right aligned ?

    Thanks for any advice.

    Cheers

    Goggs75

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Maybe DATESERIAL? Example please...
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  6. #6
    VBAX Regular
    Joined
    Oct 2007
    Posts
    28
    Location
    Hi Rbrhodes,

    As there is a full word document being copied there is not just one serial code. The example using two lines of the word document posted already has the following serial codes:

    WORD "3/10/07" changes to EXCEL "10/03/07", serial code 39151

    WORD "15/10/07 changes to EXCEL "15/10/07", no serial code until pressing F2 then enter. Then has a serial code of 39370.

    As it works manually using the commands Edit, PasteSpecial, Text there must be something missing from the following VBA commands that can tell VBA the date it is copying is in UK format not US format.

    Activesheet.PasteSpecial Format:="Text" (per macro recorder, does not work)
    Activecell.PasteSpecial Paste:=xlValues (not working either)

    Thanks for any advice.

    Cheers

    Goggs75

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I guess you can fix it after the paste.

    e.g.
    [VBA]Sub ChangeDateFormats()
    Dim cell As Range
    For Each cell In ActiveSheet.Cells.SpecialCells(xlCellTypeConstants)
    Debug.Print cell.Address, cell.NumberFormat
    If cell.NumberFormat = "m/d/yyyy" Then cell.NumberFormat = "dd/mm/yyyy"
    Debug.Print cell.Address, cell.NumberFormat
    Next cell
    End Sub
    [/VBA]

  8. #8
    VBAX Regular
    Joined
    Oct 2007
    Posts
    28
    Location
    Hi,

    I managed to fix the dates after the paste using code similar to Kenneth's, this has solved the problem.

    I am still feeling there should have been an easier and smoother way to solve the problem as there was no problem carrying out the procedure manually and VBA should have been able to do this also.

    Thanks for your help.

    Cheers

    Goggs75

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Be sure to mark your thread solved using the thread tools at the top of the page please.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    There might be some other ways to do it. I would have to know how you did it manually. Example doc and xls would help. At worst, one can use SendKeys.

Posting Permissions

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