+ Reply to Thread
Results 1 to 5 of 5

Loop through Code that copies the content of a text file to another text file

  1. #1
    Registered User
    Join Date
    08-26-2012
    Location
    amsterdam
    MS-Off Ver
    Excel 2003
    Posts
    34

    Loop through Code that copies the content of a text file to another text file

    Hi all,

    I have the following VBA code to copy the content of one text file to another text file. However I want to do that for fifteen files using a loop.

    Please Login or Register  to view this content.
    I tried to change it in the following way, but I bump into the run time 1004 error:

    Please Login or Register  to view this content.
    Does somebody has any suggestion how to adjust the code so that it runs smoothly? Thanks in advance

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Loop through Code that copies the content of a text file to another text file

    You declare csA & csB as Integer, yet it looks like you're assigning a file name to them - that'll be your 1004 error, probably.

    I would assume that the lines
    Please Login or Register  to view this content.
    should be moved into the 'For x = ...' block.

    The variables sBefore & sAfter are declared but never set to any value, so why are they there?

    Comments only - your code has not been tested.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Loop through Code that copies the content of a text file to another text file

    @ cytop,

    Observing Posts with, "eyes shut wide open", well done!
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Registered User
    Join Date
    08-26-2012
    Location
    amsterdam
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Loop through Code that copies the content of a text file to another text file

    Thanks Cytop, that seems to be the problem. Is there a reason for the fact that you have to move the variable to the for x block? I could imagine that it would also work if it was stated before the for x block!

    Regards,

    Michiel

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Loop through Code that copies the content of a text file to another text file

    No, it'll throw an error as x is undefined at that point, so will have a default value of either 0 or a null string, and both are invalid when users to refer to a cell - I missed that one!

    Also, x is incremented in the For Loop, but the assignment to the variables csA & csB is outside the loop so they would never get updated. All assuming x had been initialised in the first place.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA code for aligning excel file converted from text file.
    By anbub1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2013, 12:44 PM
  2. Import text file into Excel and using a loop to count unique words within the text
    By mrgriff21 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2013, 03:17 PM
  3. Replies: 13
    Last Post: 11-29-2012, 08:25 PM
  4. VBA code to import text file to the fixed excel file
    By wangdian in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-30-2010, 10:13 AM
  5. looping, Open text file, copy text, close text file
    By jwilds1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-18-2009, 05:12 PM

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