+ Reply to Thread
Results 1 to 22 of 22

Import and export txt files automatically with correct name/subject code

  1. #1
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Import and export txt files automatically with correct name/subject code

    Hello!

    I have 12 txt files per participant for 10-110 participants. I would like to import them to Excel so I can analyse them.
    It takes quite a lot of time to import them manually one-by-one. Is there any routine/program I could use to import them all at once? (they all have different subject codes/file names)

    I also have the problem the other way around, I need to save Excel sheets as txt files. I recorded a macro for it which works fine. However, I have to change the subject code in the makro for every single participant. Is there a kind of placeholder that I can use to fill in subject codes automatically to save the files with the correct name?

    Thanks a lot!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    It is very easy to do using simple logic and looping with Dir, if there is some logic to how the 12 text files are named. OR you can be prompted to select the 12 files. Do you want them just to be opened, or do you want to combine them in some way?

    To get the name for a save as a txt file:

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Hello!

    Thanks for the reply!

    The logic for the text files is as follows:

    There are 6 different tasks ("170905 VER2", "170905 VER4", "170905 VIS2", "170905 VIS4", "170905 SOC2", "170905 SOC4")
    that come each in 2 different types ("170905 VER2_1", "170905 VER2_2", "170905 VIS2_1", etc),
    thus 12 different file names per subject.
    The subject Code is always appended in the beginning of the file name ("PM02170905 VER2_1", "PM02170905 VER2_2", etc).

    It would also be okay to get prompted for the file names.

    I'd like them to be opened and filled in the rows (text file is Tab delimited).

    It would be great if I could have two Excel Maps:
    One in which each file is imported in a seperate sheet. (12 sheets per participant, thus 120 sheets for 10 participants)
    And the other one, in which all 12 files from one subject are imported below each other in a single sheet. (1 sheet per participant, thus 10 sheets for 10 participants)

    Thank you!

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    For each of these, select the files of interest when prompted. (You could also process the contents of a folder automatically, if all the files needed to be opened - we can do that later). I have assumed that the participant ID is the first 4 characters of the file's name.... If you need to identify which file the data was extracted from for the combined sheet we could put the file name into a column for every row from that file.


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Thank you! The two import makros work perfectly!!! Thanks a lot!

    I have a question concerning the export code though from your first reply. I tried it out and it prompts me to fill in the file name for the Excel map when saving it.
    However, I have one Excel map per participant with lots of different sheets. Among them, there are 18 sheets which are named according to the name logic discussed in my last post ("170905 VER2_1", "170905 VER2_2", "170905 VIS2_1", etc.....). I want to save these 18 single Excel sheets as signle text files tab delimited with the name of the corresponding sheet plus the participant code appended.
    Thus, the result is 18 text files with names such as: "170905 VER2_1_PM01.txt".
    Is this possible with only typing in the participant code once for all 18 files?

    Thank you!

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    Of course.

    Something like this, which will create the files in the same folder as the workbook:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Thanks, works great!!

    Is it possible to replace the

    For Each Sh In Worksheets

    in the code with something like

    For ActiveWorkbook.Worksheets("VERBAL_4N", "VISUAL_4N", etc...)

    cause I would only like to save some worksheets of the Excel file to text files. As they are always the same for every participant, it would be great to have them included in the code.

    Thank you!

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    Easy - but if there is some naming convention that could be used with some simple logic instead (e.g., the sheets that need to be exported all have underscores in their names and the others don't) that would be better - I hate to use hard-coded sheet names


    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    great, thanks!
    I am afraid there is no logic in naming that could distinguish the files I need to export and the files I don't at the moment.
    I will think of that next time! Thank you very much for your fast and really helpful code!!

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    Thanks for letting me know that it is all working out for you - have a great weekend!

  11. #11
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Hello, I have a follow-up question to this makro. The exported txt files are saved in the following format:

    FileFormat:=xlTextMSDOS

    But characters like ö,ä, ü can not be displayed in the exported text files. I guess it is due to the chosen format. Can I save it in other formats?
    If so which are my options that would display ö,ä,ü correctly? I would have to try th different formats out as I read the text files in another program which is pretty choosy for formats...
    What would be the code for that to change in the makro?

    Thank you very much! The makro is great help for me!

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    Try using

    FileFormat:=xlUnicodeText

    or

    FileFormat:=xlCurrentPlatformText

    You may need to create the strings with the required characters in VBA and write them out to the file using standard file IO commands. Post back if neither of the above work.

  13. #13
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Thanks!
    Export works perfectly with xlUnicodeText, all ä s are fine.
    Will still try if the other program can handle xlUnicodeText.
    Thank you!!!

  14. #14
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Hello,

    the xlUnicodeText did not work with the other program, but the FileFormat:=xlCurrentPlatformText worked very well- thank you!

    One last question regarding this matter:
    Using a Mac book, the export does not work, the line "ActiveWorkbook.SaveAs......FileFormat:=xlCurrentPlatformText gets highlighted and the error message appears " Laufzeitfehler "1004": Zugriff auf das schreibgeschützte Dokument nicht möglich."
    Do you know how to get it running with a mac book?

    Thank you!

  15. #15
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    Sorry - I don't know any of the ins and out of Excel for Macs. You might want to post a new message so that more people see it.

  16. #16
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Thank you!

  17. #17
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Hello, I have another follow-up question to the makro 'Sub ImportTXTFilesToSeparateSheets()'.

    I use this makro a lot and it saved me so much time, so I d like to adapt it a bit to also use it for another study but I dont understand enough of the code to do so. Two main issues came up:

    1.) The columns of the txt file are seperated by "space" this time and so if I use the makro, all columns are written in the first one and not spread out in different columns. Is there a way to adapt that in your makro? Could you tell me how, so next time my data might be seperated by comma for example I could adapt that myself- that would be great!

    2.) The worksheets are named automatically accoridng to the first letters of the txt. file in your makro. However, in my current study, the initial letters of the txt files are always the same, they only distinguish with the last letters. Is there a way to adapt that in your makro? Could you tell me again how, so next time I could also name my worksheets accordingng to the 3.-6. letter of teh txt file's name for example.

    Thank you yo much!!

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,756

    Re: Import and export txt files automatically with correct name/subject code

    If you want further help, you should remove the SOLVED tag from the thread to show that help is required again.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  19. #19
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    This uses commas and spaces to separate the text, and uses the 3rd through 6th letters to name the sheet:

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Thank you! It Works great!
    Just one question, why does it use the 3rd through 6th letters to name the sheet when you write 3, 4?
    And another one: If I write StartRow=3 e.g., the txt will be importet in A3 instead of A1?
    Thank you!!

  21. #21
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,943

    Re: Import and export txt files automatically with correct name/subject code

    Mid uses String, StartChar#, #OfCharacters - so Mid(string,3,4) returns the 3rd, 4th, 5th, and 5th characters.

    Changing to StartRow:=3 will not place the file into A3, but will result in the first two rows of the file not being imported. If you want the file to start in A3, open it then insert two rows above the data, like

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    08-04-2017
    Location
    Munich
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Import and export txt files automatically with correct name/subject code

    Great, thanks! Everything works perfectly!

+ 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] Help comparing two files by subject+Date, subject+DatePlus1, Subject+DateMinus1
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-11-2017, 07:37 AM
  2. How to export/import VBA code from one workbook to another?
    By jGKpZ8a33sPrnqX8fam7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2017, 01:26 PM
  3. [SOLVED] Need Additional Code To Automatically Import New XML Files From Folder
    By BrantBrad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-12-2015, 12:54 PM
  4. Automatically Import .CSV, Edit, Export as .CSV?
    By shottsn in forum Excel General
    Replies: 7
    Last Post: 05-04-2015, 04:08 PM
  5. Replies: 1
    Last Post: 02-18-2015, 06:56 PM
  6. Replies: 0
    Last Post: 01-28-2013, 02:25 PM
  7. Automatically sub-total a list and export to files
    By Kostanzas200 in forum Excel General
    Replies: 0
    Last Post: 01-30-2008, 10:25 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