+ Reply to Thread
Results 1 to 8 of 8

Worksheet.Opentext failure?

  1. #1
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Worksheet.Opentext failure?

    I am struggling with the Worksheet.Opentext code required to open a text file in excel - specifically with the date format.
    For example, I have a text file containing Feb08.
    When opened manually using space as deliminator it correctly returns 01/02/2008.
    However, when I record the same opening action as a macro and then run same macro it returns 08/02/2009, which is incorrect. Try it and see for yourself!
    Any ideas how to correct this apparent anomally so that the macro code returns the correct date?
    Many thanks
    Astrikor
    Last edited by astrikor; 01-12-2009 at 03:23 PM. Reason: better title

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,483
    Try What??
    You probably need a year in the date

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    It would help to see your code.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437
    Seems like the FieldInfo information is not applied when playing the macro back.

    You may need to use a QueryTable approach to load your file into an existing workbook.

    If you need more help then code and example file would help. also details of xl version.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Workbooks.OpenText macro failure? - sample code

    Quote Originally Posted by davesexcel View Post
    Try What??
    You probably need a year in the date
    Thanks for your replies.
    Not being a formally trained excel user, I rely heavily on the macro recording function for creating code. The Workbook.Opentext code appears to be fairly complex, so I am hoping someone will point me to a solution for this issue. The easiest way is for me to explain how I got the results I have described so you can do the same.

    Let me be specific:
    Open Notebook and type feb08. Save the txt file.
    Open excel (I am assuming excel 2007) and start the record macro facility.
    Now (within excel) open the saved txt file.
    When prompted, select 'Delimited' > next, 'Tab' >next, 'General', 'DMY' > Finish
    You should have Feb-08 displayed in the cell and 01/02/2008 in the editing box. THis would be correct.
    Stop the macro recording.
    Close the saved file.
    (The macro code you have recorded should resemble as below)

    Now run the macro you have recorded.

    You will have 08-Feb displayed in the cell and 08/02/2009 in the editing box. This is wrong.

    Now you will understand the problem.

    Unfortunately I have no control over the text files I am opening, so I can't change the text data format.

    If you can try the procedure I have described, assuming your results are the same as mine, presumably there is some code missing from the macro.

    Example code as follows:

    Please Login or Register  to view this content.

    This produces 08/02/2009 whereas it should be 01/02/2008.
    What is wrong?

    Suggestions welcome!

    Many thanks
    Astrikor.
    Last edited by astrikor; 01-10-2009 at 03:06 PM. Reason: code added

  6. #6
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109
    Quote Originally Posted by Andy Pope View Post
    Seems like the FieldInfo information is not applied when playing the macro back.

    You may need to use a QueryTable approach to load your file into an existing workbook.

    If you need more help then code and example file would help. also details of xl version.
    Please see my last posting for gory detail with code now added.
    (it also seems I was confused by quoting worksheet.opentext when I meant workbooks.opentext).
    Thanks
    Astrikor

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437
    Did you try my suggestion of using the Querytable code instead of OpenText?

    Turn of the macro recorder and use Data > Import External data.

    As I said it looks like this part of the code is not applied with replaying the macro,
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109
    Quote Originally Posted by Andy Pope View Post
    Did you try my suggestion of using the Querytable code instead of OpenText?

    Turn of the macro recorder and use Data > Import External data.

    Many thanks AndyPope for the route into Text via Import External Data (actually Get External Data>From Text) in Excel 2007.
    It works a treat and now correctly returns 01/02/2008 instead of 08/02/2009.
    I have attached the code for information:
    Please Login or Register  to view this content.
    Thanks again
    Astrikor

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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