+ Reply to Thread
Results 1 to 29 of 29

Importing quoted text file

  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Importing quoted text file

    in my first attempt to solve this problem I believe I asked in a too complicated way.
    For that reason I removed the question here and restated it in a post below.
    Last edited by dschmitt; 05-20-2010 at 05:16 AM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Automatically import text file

    hi dschmitt
    do you have the array values?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Automatically import text file

    I am now busy with another project. I will get back to this one in a few days.
    Thanks for your help.

  4. #4
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    Now I have the time to get back to this problem. And I want to try a new approach.

    I have a macro that can import several XML files. I want to change the macro so that it can import instead several comma delimited text files.

    To illustrate I attached an Excel file called example.xlsm which contains the macro. I also attached 2 xml files. Please change the the file names from .txt to .xml. With these xml files you can test the macro. You can execute the macro by clicking on the AuthorAnalysis button on the far right in the Home menubar. This macro is working fine.

    I also attached 2 comma delimited text files (1.txt; 2.txt). These are the files that I want the new macro to import.

    I tried to solve this problem by first using the macro recorder followed by using the macro recorder code to modify the xml import macro. Unfortunately I was not successfull.
    Attached Files Attached Files
    Last edited by dschmitt; 05-20-2010 at 10:01 PM.

  5. #5
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    boiling down the problem to the essential ...

    how would I have to change the following code so that it will import a comma delimited text file?

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    below is the code of the entire macro.

    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    I am trying another approach to get a solution to my problem.
    Attached is an Excel file with the macro and one comma delimited text file.

    The following code gives me an error in the first line of the Import file code

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by dschmitt; 05-20-2010 at 10:07 PM.

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    Hi dschmitt
    I havent had a look at the file but the basic code to import text files
    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    pike, if you want to help then please look at the problem in my last post, which is the one before yours. I think if I can get a solution to this problem then I can do the rest.

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    I like my way better as it will let you sort values ect.
    dont know what in the txt files but try..
    Please Login or Register  to view this content.
    Last edited by pike; 05-20-2010 at 04:18 AM.

  11. #11
    Registered User
    Join Date
    05-20-2010
    Location
    Fresno, CA
    MS-Off Ver
    2003, 2010
    Posts
    58

    Re: Problem with importing quoted text file

    dschmitt,

    Your problem was that you were attempting to pass an array of values (file names to import) to a procedure that can only process one file at a time. The solution is to add a loop, so that each value in your array gets processed. I attached an example of a loop that will import multiple files into a separate worksheet. However, let me be honest upfront and admit that I was lazy, and included no error handling procedure for dealing with a data array (number of files to import) that is greater than the number of worksheets in your workbook. It's late and I was concerned with resolving your original problem.

    I hope this helps. Try running the attachment with the two attached text files (your text file 1.txt, which I copied and named 2.txt).
    Attached Files Attached Files
    Last edited by Ryan Murtagh; 05-21-2010 at 02:14 AM. Reason: Replaced text file attachments
    Thanks,
    Ryan



    A word to the wise is infuriating. - Hunter S Thompson

  12. #12
    Registered User
    Join Date
    05-20-2010
    Location
    Fresno, CA
    MS-Off Ver
    2003, 2010
    Posts
    58

    Re: Problem with importing quoted text file

    Ok, I felt bad, so I added some validation to handle instances where the number of files selected exceeds the number of worksheets in the workbook.

    Your code for importing the text files and applying the text to columns function has Macro Recorder written all over it. I don't have much experience programmatically importing text files, so I left everything after your "import the file" comment alone.



    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    Ryan, no need to feel bad. You gave me what I was asking for. Pike, I have to digest your code first. It looks appealing.

    Below is what I needed!

    Please Login or Register  to view this content.

    And below the working whole sub.

    Please Login or Register  to view this content.

  14. #14
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    your a bright spark and picking up coding very easy
    always good to see a thinker that has a go

    and updates the threads to solve thanks for posting your solution I'm sure it will help others

  15. #15
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    Ryan wrote:
    Your code for importing the text files and applying the text to columns function has Macro Recorder written all over it.

    For that reason I would like to give pike's code a try.

    both of you, thanks for your help and encouragement.

  16. #16
    Registered User
    Join Date
    05-20-2010
    Location
    Fresno, CA
    MS-Off Ver
    2003, 2010
    Posts
    58

    Re: Problem with importing quoted text file

    Very happy to help. Thank you for posting your completed procedure, and good luck with your project!

  17. #17
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

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

  18. #18
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    people may notice that I use 2 personal buttons in the Home menubar and wonder how that is done.

    2 files are involved. These files are burried in the xlsm file. To get to them you need to rename the xlsm file to a zip file. Then you can look into the package content of the xlsm file using the Windows File browser (use column view).

    From my example.xlsm file copy the .rels file and paste it into your xlsm file. Then copy my customUI.xml file onto your desktop. Edit this file as needed and paste it over the one in your xlsm file.

    Finally you need to write Subs to link the menubar buttons with your macros. For that see example.xlsm Module3.
    Last edited by dschmitt; 05-20-2010 at 10:47 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    pike, your code does not import the quoted text file properly. What should go into one column is spread over several columns. You can check the results of your code with the files attached. I simplified the quoted text files to make the error more easily visible.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by dschmitt; 05-21-2010 at 12:45 AM.

  20. #20
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    Hi dschmitt
    the first code split the file lines up with the "," delimiter
    this will just copy the whole line and place it in one column
    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    pike, putting the whole content in column A is not a solution.
    Information for e.g. author, title, abstract need to be put into individual columns.

    The problem is that your code does not distingish between the comma delimitors and the commas in the text. For that reason, when ever there is a comma e.g. in the abstract the text is broken up and separated into new columns. So, you end up with the abstract spread over x number of columns. x being the number of commas in the abstract plus 1.

    The code obtained from the macro recorder does distingish and imports properly.

    Now, I don't want to use up your time. In general the problem is solved. However, if it bugs you that your code does not do the job yet, I would appreciate if you would work on it. Your code certainly looks better than the macro recorder code.

  22. #22
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    Hi dschmitt
    how were the txt file created?

  23. #23
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    the quoted text files were created by a commercial database.

    What your code needs to do is ignore the commas within two " marks.
    For example:
    "abc","def","g,hi","jkl"
    Your code needs to ignore the comma between g and h.

  24. #24
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    ok... opened the txt file
    and
    Please Login or Register  to view this content.
    Last edited by pike; 05-21-2010 at 05:12 AM. Reason: use x as i in use

  25. #25
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    the only problem left now is that in each cell the content is bracketed with " marks.

    For example

    A
    1 "Title"
    2 "This gene..."

  26. #26
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem with importing quoted text file

    back cooking dinner
    arh
    Please Login or Register  to view this content.
    what the code does is looks at every letter to build up the text
    once done it will strip off the first and last "

  27. #27
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    pike, I found a new error. This one is most likely the last one.

    In the case of one blanc cell the code does fine
    e.g.
    "514,"","2010"

    however, sometimes towards the end of the columns several cells are blanc.
    In that case the macro crashes.
    e.g.
    "514","2010","","","","","","","","","",

    I attached a sample data file.

    I feel bad that again I let you do this problem. It sounds like not to difficult to solve. But yet again I have no time to work on it.
    Attached Files Attached Files

  28. #28
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with importing quoted text file

    I found the time to have another look at the remaining problem. Below is the solution.

    Please Login or Register  to view this content.
    And below the whole sub. I attached a sample quoted text file.

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

  29. #29
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Importing quoted text file

    Hey dschmitt,
    well done, thanks for posting back with the solution.

+ 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