+ Reply to Thread
Results 1 to 7 of 7

Macro to copy from a text document into excel document

  1. #1
    Registered User
    Join Date
    07-24-2012
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    4

    Macro to copy from a text document into excel document

    Greetings,

    I have to copy the same information from the same type of text document over and over into the same excel document to create datasheets. I have used VBA to create a streamwriter in a stand alone application but have never used one in conjunction with excel. I am having problems with figuring out how to open the text document to copy and move the focus to different cells in excel.

    Thanks

    jroder

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to copy from a text document into excel document

    I would think you would need to provide a couple of sample text files and a sample Excel file where you've mocked up your needed results from those text files so we can see the BEFORE and AFTER. Then we can suggest methods of bridging the gap.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-24-2012
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro to copy from a text document into excel document

    Here are Before and After excel files for everyone to look at and the text file to copy from:

    The items needed from the text file are:

    EPHEMERIS
    SOFTWARE
    START
    FIXED AMB
    OVERALL RMS
    THE ERROR FOR:
    LAT
    W LON
    EL HGT
    ORTHO HGT

    They need to be moved over to the excel file in the cells labeled with their names. Every excel file will be the same and every text file is in the same format. I have tried to splice the text file up with the text to columns function and then recording a macro but there is not an easy way to divide the text because of all the spaces.

    Thanks,

    jroder

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro to copy from a text document into excel document

    Please Login or Register  to view this content.
    David
    (*) Reputation points appreciated.

  5. #5
    Registered User
    Join Date
    07-24-2012
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro to copy from a text document into excel document

    David,

    Thanks for the reply especially since I accidentally posted this thread twice once in into forum.

    The code is opening the file but for some reason iFound is staying at 0 on my Excel 2003 and then it is not cycling through the cases and just going straight to the end, can i use a different counter to do the job? I am trying to fix it myself, I will post another thread if i am able to get it. Also I am just curious what Dim Temp$ means?

    Thanks,

    jroder

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro to copy from a text document into excel document

    Oh, you want me to EXPLAIN everything? That'll cost ya! Hahaha! just joking!

    This code worked on the sample file you u/led.

    I'll have another look this evening and comment the code for you.

    But for now, Dim Temp$ is the same as Dim Temp as String. It's shorthand left over from the early days. Each variable has it's own shortcut symbol.

    Temp$ is just a temporary variable that is taking the text file in one line at a time. (Line Input)

    Once I have Temp$, I check to see if our keyword (SOFTWARE: ) is anywhere in that line. (InStr) If it is, InStr will return the Starting Position of the match. So if iFound is anything other than 0, then we have a match. If it is 0, I loop to the next keyword until I check all the keywords, then read the next line and do it all over again.

    Once a keyword is found, the Select case branches the code to the correct line to parse the info we need and post it to the spreadsheet.
    Last edited by Tinbendr; 07-25-2012 at 04:54 PM.

  7. #7
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro to copy from a text document into excel document

    Oops, sorry. I had the InStr function backward. And You! You have merged cells! A cardinal sin of Excel worksheets. I had to add a function to handle those devils.

    It seems to be working now. Some of the info might not be formatted just right, so tell what needs to be fixed.

    Please Login or Register  to view this content.

+ 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