+ Reply to Thread
Results 1 to 9 of 9

Looping Through Txt Files in Folder and Pasting Contents to Worksheet

  1. #1
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Hello,

    I am new to VBA and am wondering how I can loop through a folder containing multiple .txt files and paste the data into worksheet cells. These are tab delimited files.

    Some of the requirements are..

    1. I want to start reading each .txt file from the second line. The .txt file has headers that I don't want in the worksheet.

    2. Each row of the .txt file needs to be placed into its own cell, going down a single column. For example, if the .txt file has 5 rows, the worksheet will be filled (A1:A5)

    3. Each file in the folder will be separated by a blank row.


    Any help would be really awesome! Cheers.
    Last edited by bruppert; 05-07-2019 at 04:31 PM.

  2. #2
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Clarification, the files can be separated in a different way. Doesn't need to be a single blank row.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Hello Bruppert,

    This macro let's you select the folder with the text files to be imported. The files are copied from line 2 to the end of the file onto worksheet "Sheet1", starting with cell "A1". After the file is copied, the next row reads "(End Of File")". Blank rows are not reliable markers as the file itself could have blank rows. Only files with ".txt" extensions are opened. The macro can be changes to open more than one extension e.g. "*.txt;*.csv" for text files and Comma Separated Values.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    This is great! Thank you Leith.

    If I wanted the files to be stacked on top of each other with no separation between them in the worksheet, how would I go about this?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Hello bruppert,

    Change this line from...
    Please Login or Register  to view this content.
    Change this line to...
    Please Login or Register  to view this content.
    To remove the blank row separating the files after (End Of File)

  6. #6
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Perfect. Thank you.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Hello bruppert,

    Glad I could help. I am going to mark this post solved, unless you have some other questions.

  8. #8
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Last question,

    How would I update the code so that there is no separation at all between the .txt files in the worksheet?

  9. #9
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Looping Through Txt Files in Folder and Pasting Contents to Worksheet

    Never mind, found out how to do this. I first resized the range to (n,1) and then offset the range to (k-2,0). This post is solved!

+ 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. looping through a folder, sub folder and files
    By Brazz1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-15-2019, 03:05 PM
  2. [SOLVED] Looping thru files in folder
    By rpinxt in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-22-2018, 06:02 AM
  3. Looping through a folder of files in VBA
    By lizzo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2013, 05:29 PM
  4. looping through files in a folder
    By bored in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2012, 02:20 AM
  5. Looping through files in a folder
    By pdb78 in forum Excel General
    Replies: 4
    Last Post: 01-27-2012, 12:16 PM
  6. Help with VBS code for looping files in a folder
    By johncassell in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2011, 05:19 AM
  7. Looping through files in a folder
    By prepotency in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2005, 11:23 AM

Tags for this Thread

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