+ Reply to Thread
Results 1 to 11 of 11

Loop through folder with txt files, import them and save as xls file

  1. #1
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Loop through folder with txt files, import them and save as xls file

    Hi

    I am trying to write a macro that will loop through my folder of txt files, import them that will use the import feature and delimit it, then save as an xls file.

    So far, I have only been able to use the following, but it only allows me to import one file (and I know the folder path, I want to avoid choosing the folder).

    It also adds many quotation marks ("") when I only want to import using tab delimiter and comma.

    Can anyone help me with this?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Loop through folder with txt files, import them and save as xls file

    I am trying to write a macro that will loop through my folder of txt files
    Q - every file?

    import them
    Q - into one sheet\ each text file to different sheets\different workbooks?

    It also adds many quotation marks ("") when I only want to import using tab delimiter and comma
    please attach a sample typical text file to make testing possible - easier to play with the real data

    to attach a file
    - click on Reply
    - Click on GoAdvanced
    - look below fo ManageAttachments etc

    thanks
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Hi

    yes it is every file to loop through in the folder. They are all txt files.

    They should come into separate workbooks, i.e. import one txt file into one workbook (one sheet) then save it as xls (same file name), close and open next file and so on. They all need to be saved in the same folder that the txt files are currently in.

    I hope that explains it properly.

    Attached is a small sample. The way I import using the wizard is to use delimited, comma, general, finish.
    Attached Files Attached Files
    Last edited by maym; 10-03-2017 at 07:20 PM.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Loop through folder with txt files, import them and save as xls file

    Hi maym,

    If you were using Power Query, this would be an easy problem. See the attached. Is this what you want? Or will you only accept a VBA answer?

    SampleAnswer.xlsx
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Hi Marvin

    thanks for your reply. I am looking to do this through VBA if possible?

  6. #6
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    I have found something that looks like when executed, will give me close to the desired result:

    Please Login or Register  to view this content.
    However I get a 'compile error: invalid use of new keyword' on this line:

    Please Login or Register  to view this content.
    Can anyone assist with this please?

  7. #7
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    I have managed to put together the following. However as shown in the sample txt file, the dates should show as 7/1/2013 (1 Jan) but when I re-open the file, they show as 1/7/2013 (1 July).

    How can I solve for this??

    Please Login or Register  to view this content.
    Last edited by maym; 10-05-2017 at 07:19 AM.

  8. #8
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Anybody know how I can solve this???

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Loop through folder with txt files, import them and save as xls file

    Your thread somehow disappeared off my subscribed threads list - so has not been on my radar - I will have a look later today/tomorrow and update the thread after that
    kev

  10. #10
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Quote Originally Posted by kev_ View Post
    Your thread somehow disappeared off my subscribed threads list - so has not been on my radar - I will have a look later today/tomorrow and update the thread after that
    kev
    perfect, thanks so much kev

  11. #11
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Loop through folder with txt files, import them and save as xls file

    Unfortunately with Workbooks.OpenText, Excel brings in date&time values as numbers interpreted via application default - and Excel's native tongue is American!
    (As far as I know) there is no way to fix the code to allow Workbooks.OpenText to be used and get the dates correct

    Therefore forced to import the data, where everything is brought in as text
    - this method adds a connection to the text file (not intending to update later - so connection is removed by VBA)
    - the date & time values are now text (does not matter unless you need to perform calculations or sort)
    - if that is a problem then just need to a add a function to convert date&time text to values (do you need this?)
    - I noticed that columns L and N are better treated as numbers and added a routine to convert those values to numbers
    - that simple routine unfortunately does not work with the date&time text (more precisely - it works but gets the answer wrong )
    - other columns left as text
    - your original VBA (post#7) was creating xls files, whereas this creates xlsx files (this should not be an issue given that you are using Excel 2010)


    Test in attached file with {CTRL} k

    Place this code in a general module:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kev_; 10-06-2017 at 02:47 AM.

+ 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. [SOLVED] Loop through files in folder, email file to listed recipient of file name = cell value
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-04-2017, 03:41 PM
  2. [SOLVED] Import data from files in folder with a given word/text in the file name
    By runnerD2016 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2016, 11:20 AM
  3. [SOLVED] Loop through multiple files, run macro, then save as Excel workbook in a different folder
    By Peter Kallio in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-26-2013, 11:42 PM
  4. Replies: 0
    Last Post: 04-16-2012, 10:57 AM
  5. Save File As Cell Value (loop through folder)
    By uncleslinky in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2012, 04:02 AM
  6. Replies: 17
    Last Post: 12-06-2011, 11:50 AM
  7. Import all access files from a folder to a single excell file
    By wali in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2009, 05:04 AM

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