+ Reply to Thread
Results 1 to 10 of 10

Open .TXT file Replace Specific Lines with Cell Values

  1. #1
    Forum Contributor
    Join Date
    11-28-2005
    Location
    Dover, England
    MS-Off Ver
    365
    Posts
    172

    Open .TXT file Replace Specific Lines with Cell Values

    Daily, I have a large number of .txt files which I need to open and replace the contents of line 8 with a cell value and 9 with the character I. It will always only ever be lines 8 & 9 of the txt file and although line 8 is a changing string, line nine will always be the character I. At present I am doing this manually but life is so short I am looking for a more automated solution.

    I do have all the files are listed to be changed within a worksheet, but other than that i m completely stuck. Any help would be greatly appreciated.

    Thanks
    Last edited by Tellm; 11-09-2011 at 11:57 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Open .TXT file Replace Specific Lines with Cell Values

    Please attach a few sample text files and the excel file that has the list of files. It will be easier to help you.

  3. #3
    Forum Contributor
    Join Date
    11-28-2005
    Location
    Dover, England
    MS-Off Ver
    365
    Posts
    172

    Re: Open .TXT file Replace Specific Lines with Cell Values

    Apologies for not responding earlier, I've been out today. Please find the files and spreadsheet as requested. The file detail is pasted in from another file. I have attached a few sample files. The character I, in line 9, is to replace the character B.

    Many thanks
    Attached Files Attached Files

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Open .TXT file Replace Specific Lines with Cell Values

    Please Login or Register  to view this content.
    Last edited by snb; 11-08-2011 at 01:28 PM.



  5. #5
    Forum Contributor
    Join Date
    11-28-2005
    Location
    Dover, England
    MS-Off Ver
    365
    Posts
    172

    Re: Open .TXT file Replace Specific Lines with Cell Values

    snb, Many thanks for this I will look to testing this a little later this evening. Are you able to code this so that sp(8) looks up the cell value in C3. Regards

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Open .TXT file Replace Specific Lines with Cell Values

    I suppose you mean the value in column C in the same row.
    NB the 8th line is in sp(7)
    the 9th line in sp(8)
    Please Login or Register  to view this content.
    Last edited by snb; 11-08-2011 at 04:18 PM.

  7. #7
    Forum Contributor
    Join Date
    11-28-2005
    Location
    Dover, England
    MS-Off Ver
    365
    Posts
    172

    Re: Open .TXT file Replace Specific Lines with Cell Values

    snb, once again thanks for your efforts. At present I cannot get this to work - when I step through I get a run-time error 9 Subscript out of range box pop up at the point sp (7) =sn(j, 3)

    I think I might have confused things but the cell value of C3 (currently Flat100*10) needs to be written to line 8 of the NC files currently populated with PLT10

  8. #8
    Registered User
    Join Date
    08-25-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Open .TXT file Replace Specific Lines with Cell Values

    Not the cleanest code in the world, but it should be a decent base. I seperated out the file name, just in case you don't actually save the files in Temp. If you do save it in that directory, you can change "C:\Temp\" & Mid(fileName, 8) to just fileName.

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    11-28-2005
    Location
    Dover, England
    MS-Off Ver
    365
    Posts
    172

    Re: Open .TXT file Replace Specific Lines with Cell Values

    J - that's perfect many thanks

  10. #10
    Forum Contributor
    Join Date
    11-28-2005
    Location
    Dover, England
    MS-Off Ver
    365
    Posts
    172

    Re: Open .TXT file Replace Specific Lines with Cell Values

    J - I spoke to soon. I used the code this morning and it worked great on rows 5-13. But the number of files varies from one day to the next whereas 1 day I might have 2 files and another I could have up to 60 rows. I look at the code and changed it so that it read lines 5-70 but then I get an error message at the point of the last file name. Are you able to adapt this so that it only loops through the number of files I have in the list and then stops. Regards

+ 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