+ Reply to Thread
Results 1 to 28 of 28

Importing multiple text files into multiple worksheets

  1. #1
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Importing multiple text files into multiple worksheets

    I am setting up a spreadsheet to analyze data from a program I use. The files are with extension .npe, but to upload it, I added the .txt.

    I have the following worksheets: Input, Run Selection, Summary, and R1 through R20.

    On the input tab are the path file locations, starting in C6 and down to C25 (up to 20 files). The paths are from another workbook, and brought in automatically to the Input spreadsheet.

    I want to write a VBA code that reads the info in C6-C25, and writes the text from those files into sheets R1-R20. However, there may be 2 files, 13 files, or 20 files.

    I have most of the code written, but I am having the hardest time figuring out how to read in each of the files and put them into the individual sheets. I have found code that comes close, but the issue is that the code is really extensive, and I'm not sure I need most of it.

    I have attached a sample text file, and the coding I have so far. Any assistance would be appreciated.

    Thanks
    Attached Files Attached Files
    Last edited by new.vbacoder; 09-26-2010 at 03:10 AM.

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

    Re: Importing multiple text files into multiple worksheets

    Please Login or Register  to view this content.



  3. #3
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Thanks for your post.

    I understand how that opens a workbook, but I don't want to hard code the text file, as it changes for each scenario.

    I also want to copy and paste the info from run 1.npe to worksheet R1, and then close the .npe file. Then repeat the process for each individual run from 2 up to 20, but could include only 10.

    If it can be brought in as semi-colon and comma delimited without having to run a separate sub, that would be good too.

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    hi,

    Welcome to the Forum

    Ron De Bruin has a variety of links which should help, including the below which refer to csv or txt files. The principles should be easily adapted for other file types (eg .npe files).
    http://www.rondebruin.nl/txtcsv.htm
    http://www.rondebruin.nl/csv.htm (check links at base of page too)

    I have found code that comes close, but the issue is that the code is really extensive, and I'm not sure I need most of it.
    Don't worry about how extensive it is - others may disagree, but I think that if it is from a reputable source, you should be able to learn a lot from stepping through it with the [F8] key &/or selecting a keyword and pressing [F1].
    However, keep the words of wisdom in Snb's signature in mind
    Once you understand what the code does you can trim it back, so that it meets your requirements & no more, plus you will have learnt something along the way.

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

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

    Re: Importing multiple text files into multiple worksheets

    but I don't want to hard code the text file,
    Did I ? How flexible it must be ?

    Did you consider:

    open the first npe file.
    open the second npe file / copy the content of the second file into the first one / close the second file
    open the third npe file / copy the content of the third file into the first one / close the third file... etc.
    Last edited by snb; 09-12-2010 at 03:29 PM.

  6. #6
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by broro183 View Post
    hi,

    Ron De Bruin has a variety of links which should help, including the below which refer to csv or txt files. The principles should be easily adapted for other file types (eg .npe files).
    http://www.rondebruin.nl/txtcsv.htm
    http://www.rondebruin.nl/csv.htm (check links at base of page too)
    Thanks a ton, those helped, for the most part. The problem I'm having now, is I have a formula on the input sheet that writes the file string (up to 20 files), and I want the Sub to read in each file and paste them in the appropriate sheet. I can figure out how to do each one individually, but I'd rather put in some type of IF statement that if there is a file reference in cell C6, then put that in R1, if there is something in C7, put it in R2, etc. to C25 and R20, but if there isn't a file, end the macro.

    I also want to bring the file in with two seperators (semicolon and comma, but can't figure out where to put the second seperator in the code.

    ImportTextFile FName:=ActiveWorkbook.Sheets("Input").Range("C6"), Sep:=";"

    Thanks
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    hi,

    For the first part, I suggest:
    1) creating a dynamic named range (DNR) that expands to include the number of rows that have file names in them
    2) Creating an Array in VBA that is populated with the contents of the DNR. I'd use a variant array, but that may not be necessary...?
    3) Looping through each item (ie file string) in the array.
    Here's a rough example:
    Please Login or Register  to view this content.
    re the multiple delimiters,
    I haven't tested this, but can you modify code from a recorded macro?
    Ooopps, after actually reading your code, I realise that may not help!
    I also want to bring the file in with two seperators (semicolon and comma, but can't figure out where to put the second seperator in the code.
    -do you mean a two character separator eg ";,"?
    I think you have to modify this
    Please Login or Register  to view this content.
    - or either of the separators, scattered willynilly through the file?
    In which case, you'll need to explain the willynilly logic or, alternatively check for either Separator...
    Please Login or Register  to view this content.
    btw,
    1) I have read (somewhere? ) that in recent versions of VBA, all Integers are internally compiled as Long & therefore I suggest changing some of your variable Typing to Longs.
    2) It would be easier (for me, & I'm sure for others too) if you could upload a sample excel file (rather than a text file).


    hth
    Rob

  8. #8
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    From another angle, have you tried (recording a macro when) Excel's inbuilt functionality of the Text Import Wizard &/or Text to Columns?

    Rob

  9. #9
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by broro183 View Post
    hi,

    btw,
    1) I have read (somewhere? ) that in recent versions of VBA, all Integers are internally compiled as Long & therefore I suggest changing some of your variable Typing to Longs.
    2) It would be easier (for me, & I'm sure for others too) if you could upload a sample excel file (rather than a text file).
    It's a proprietary program, so I don't think I can upload it, just try my best to explain.

    Quote Originally Posted by broro183 View Post
    hi,
    - or either of the separators, scattered willynilly through the file?
    In which case, you'll need to explain the willynilly logic or, alternatively check for either Separator...
    Please Login or Register  to view this content.
    If you look at the txt file I previously uploaded, each line has a comma and a semicolon that I want to separate into individual columns.
    Last edited by new.vbacoder; 09-13-2010 at 09:37 PM.

  10. #10
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by broro183 View Post
    From another angle, have you tried (recording a macro when) Excel's inbuilt functionality of the Text Import Wizard &/or Text to Columns?
    hi,

    Here is some code recorded in Excel 2007 which I think is very close to what you want, by going to Data - "From Text". Perhaps you could modify the recorded code* and add it in a loop structure like my proposal in post # 7.
    * I have made no changes to the recorded code therefore there may be some "extra" code. Also, unfortunately, I think this is one of those cases where the actual code recorded differs between Excel 2003 & Excel 2007.

    Please Login or Register  to view this content.
    If you can't modify this to do what you want, can you please let us know what you get stuck on?

    We all work in various proprietary programs, but to get the most effective & contextualised solution, it's your challenge as a Poster* to create a sample file that is structurally the same as your real file but contains no proprietary information (eg it has nonsense data & any other sensitive/indentifiable information has been removed).

    *if helpers can't understand, or want to test in an actual document.


    hth
    Rob
    Last edited by broro183; 09-15-2010 at 04:39 PM.

  11. #11
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Thanks for everyone's help. Because I needed to have this solved sooner rather than later, I decided to just right the code to open each file individually (it's longer than I wanted, but it works). So it opens file1, then 2, then all the way to 20, if there are that many files.

  12. #12
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Okay, I'm sorry we weren't of more help but I am pleased you have a solution.

    Feel free to post your final solution code, because it may help others who come across this thread when searching for answers. Plus, it may give someone a "better late than never" chance to help you, by improving upon your solution.

    If you are happy with the outcome, can you please mark the thread as Solved?


    Rob

  13. #13
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Here is what I came up with:
    Please Login or Register  to view this content.
    And the Call TextToColums is:
    Please Login or Register  to view this content.
    Hope that helps
    Last edited by new.vbacoder; 09-24-2010 at 07:38 PM.

  14. #14
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by broro183 View Post
    Okay, I'm sorry we weren't of more help but I am pleased you have a solution.

    Feel free to post your final solution code, because it may help others who come across this thread when searching for answers. Plus, it may give someone a "better late than never" chance to help you, by improving upon your solution.

    If you are happy with the outcome, can you please mark the thread as Solved?


    Rob
    Where do I mark it solved??

  15. #15
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Ooopps, sorry, I had meant to include a link in my last post.

    To mark your thread solved do the following: - Go to the first post - Click edit - Click Advance - Just below the word "Title:" you will see a dropdown with the word No prefix. - Change to Solve - Click Save
    source

    Rob

  16. #16
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Edit: Hey, the code looks good - it won't be long before you don't need any help
    End Edit

    Quote Originally Posted by broro183 View Post
    ...Plus, it may give someone a "better late than never" chance to help you, by improving upon your solution...
    I'm not sure if you guessed that I would try to be the "someone", but I'm giving it a go now...

    Edit: I can't improve it much other than to save you the hassle of duplicating the code for each file, by adding in some looping.
    End Edit


    Here's a teaser, while I finish the looping:
    Please Login or Register  to view this content.
    I should have the rest up shortly - perhaps after a snack

    hth
    Rob
    Last edited by broro183; 09-24-2010 at 05:30 PM. Reason: celebrate the OP's success

  17. #17
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    try this one out...

    Please Login or Register  to view this content.
    hth
    Rob
    Last edited by broro183; 09-24-2010 at 07:00 PM. Reason: 1) added the Assumption into the code. 2) corrected booboo in "...Read As #FNum" line

  18. #18
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Quick comment. I changed the Sep to comma, and then changed the text to columns to column B. What was happening before was that it parses the semi-colon first, putting the value into column 2, and then when it does text to columns (comma separated), it overwrites that column.

    That code was great, fabulous, awesome, only two small issues.

    1. It keeps asking if you want to replace contents of the destination cells, and you have to click OK for each file. Is there a way to automatically say OK. (Update: after closing and re-opening, it no longer has that message).
    2. For some reason it duplicates the last line in the file.
    Last edited by new.vbacoder; 09-25-2010 at 01:46 AM.

  19. #19
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Yah!

    Now I'm feeling more helpful

    re 1)
    I'm pleased that message has gone away & I'm guessing it was tied in with the order of which separator you processed first. I wouldn't have recommended it in this case, but just so you know for other coding, I suggest reading about "application.displayalerts = false" in the VBE Help files.

    2) I'm not sure why this is happening?
    Did it happen in your original code?
    Is the last line in the text file a duplicate of the second last line?
    Perhaps, you could try changing your
    Please Login or Register  to view this content.
    , but I don't think that will help & I think it will error if you have any empty text files.


    Now that you have the general looping code (through each file) you could try some of Ron DeBruin's code. I haven't tested this properly, but I think it will be close & you should be able to fix anything I've missed...

    Please Login or Register  to view this content.
    hth
    Rob

  20. #20
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by broro183 View Post

    2) I'm not sure why this is happening?
    Did it happen in your original code?
    Yes, but when run a second time, it disappeared.
    I figured out the issue. The first line does not have a comma, so when I run it first with comma separated, it moves all the results up a line, and then makes a copy of the last line.
    Is the last line in the text file a duplicate of the second last line?
    No.
    Please Login or Register  to view this content.
    I noticed there is an apostrophe on the above line, which then doesn't clear info in the R sheets, and then each time the code was run it created another set of files in the sheet (i.e. if you were to run the ProcessNPE 5 times, there would be 5 copies in each sheet). I deleted the apostrophe and it worked great.

    Please Login or Register  to view this content.
    What is this stop for? When I run the ProcessNPE_v3, it stops there. When I take it out, it runs great.

    This new code seems to do the trick, without the first stop.

    Thanks a bunch

    Joel
    Last edited by new.vbacoder; 09-25-2010 at 04:26 PM.

  21. #21
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    hi Joel,

    I'm pleased I could help :-)

    Sorry about those wee glitches, when I was testing I didn't have twenty "R#" sheets so I commented out that line & forgot to uncomment it. The Stop was also for when I was testing & I forgot to remove it. When testing the macro, the Stop allowed me to press [F5] & the code would run right up to the line & then I could press [F8] to go through the remaining code line by line.

    Does the latest code look anything like your original attempts using Ron's examples?
    Do you understand what each part is doing?

    If you're happy with the solution, can you please mark the thread as Solved?
    To mark your thread solved do the following: - Go to the first post - Click edit - Click Advance - Just below the word "Title:" you will see a dropdown with the word No prefix. - Change to Solve - Click Save
    Source: http://www.excelforum.com/faq.php

    Rob

  22. #22
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by broro183 View Post
    hi Joel,

    I'm pleased I could help :-)

    If you're happy with the solution, can you please mark the thread as Solved?

    Rob
    You were great, and helped me tremendously. It works just like I need it to. I will definitely mark the thread solved.

    I have another question for you. I've been searching the threads, but haven't found quite what I'm looking for. Do you know of a thread that discusses transposing data in a vba code without first having to paste it into a worksheet? I have a file that could have more than the 265 columns worth of semi-colon delimited information, so I'd like to be able to transpose it before ever pasting it into excel. If there isn't something you could point me to, then I'll start another thread and ask for some assistance.

    Again, thanks to everyone who helped out. I think I'm getting a better handle on this whole coding thing.

  23. #23
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    Here's the final code:
    Please Login or Register  to view this content.

  24. #24
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Quote Originally Posted by new.vbacoder View Post
    ...Do you know of a thread that discusses transposing data in a vba code without first having to paste it into a worksheet? I have a file that could have more than the 265 columns worth of semi-colon delimited information, so I'd like to be able to transpose it before ever pasting it into excel. If there isn't something you could point me to, then I'll start another thread and ask for some assistance...
    Hi Joel,
    Can you please start a new thread?
    - it keeps things tidier
    If you want, you can send me a private message with a link to the new thread & I'll post some links in it.

    Rob

  25. #25
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    So, today I ran the code and get a run-time error. The issue happens when there are less than 20 files, I get a runtime error 1004, and it highlights the .Refresh BackgroundQuery:=False.

    I have 8 files currently (C6-C13), and when it gets to # 9 is when I get the error. I figure it's because there's no data to read in C14, it's blank, but there is still a formula in the cell [=IF($B$1<B14,"",'[Vissim Post Processor.xls]Project Info'!$B$5&"\"&$D$1&"_"&($B$2+B13*$B$3)&".npe")].

    Is there a line I can put into the code that if the cell value is blank, then exit the process?

    Thanks

    J

  26. #26
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    Hi Joel,


    Are you still using the code you put in post # 23?
    If so, I'm not sure why the error is occurring because I think the following line should cause the entire Refresh query table section to be skipped/bypassed if there is nothing in cell C14:
    Please Login or Register  to view this content.
    hmmm...
    I think I may be overlooking something else in the code, but I'm not sure what it is...?

    Perhaps one of the suggestions (eg using "Wait") in the below thread will help: http://www.vbforums.com/archive/index.php/t-333048.html

    If not, let me know & I'll look again but in more detail. To help investigate, is there any way you could upload the file that is causing the error (after you run the code but w/o including any confidential info)?

    Rob
    Last edited by broro183; 10-22-2010 at 11:17 PM. Reason: I'm out of practice so I had to change "vba" tags to Code tags ;-)

  27. #27
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    Re: Importing multiple text files into multiple worksheets

    I added a message box that says the file does not exist with the run number, and the user presses ok for each run missing, and then it finishes the code.

    I like it better this way than skipping files that don't exist. That way, if there are files missing, the user will know.

    Thanks

    J

  28. #28
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Importing multiple text files into multiple worksheets

    hi,

    Yes, I agree it is better to inform the user of any irregularities. Is your message box within the loop?
    If so, I'd suggest building/concatenating a string of the file names within the loop & only presenting the message box results once the loop has finished (ie near the end of the code). This approach minimises user interaction, allowing the code to complete faster & still giving the user all the information about the missing files in a single message box.


    Rob

+ 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