+ Reply to Thread
Results 1 to 46 of 46

Efficient way to read in text file and account for possibility of large number of rows

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Efficient way to read in text file and account for possibility of large number of rows

    Hi,

    I have read in files into excel but never have i tried to read in a txt file line by line. But i think this is what i need to do for this one.

    I have an input test file (see enclosed)
    I have an output file of what i would like the input file transferred to.

    Note: I am running excel 2003 for this macro. So my max number of rows is ~65000 and the input files CAN BE larger than 65000 rows.

    I am not sure how to even start reading in the file or the best way to parse the file.........any suggestions or ideas would be helpfull.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Efficient way to read in text file and account for possibility of large number of rows

    Hi,
    Just thinking "out loud" here...

    The first rows up to <EOH> seems to be the "header" of the test file. Let call this the TESTID
    Can there be multiple TESTID in the same text file?

    After <EOH> line, all results seems to start by "Site_p...." except for the one that first starts with the waferID "44332211LotID..."
    Can the be multiple WAFERID in the same text file?

    All results after seems to have the same pattern : "text, value"

    My first guess would be to do this :
    - Read line by line the text file
    - Save the data up to <EOH> in a array of 1 line x N columns
    - Count the number of <EOS> values in the text file to determine the size of a second array
    - Read the text file line by line, if after <EOH> the value countains LOTID, store this as the wafer number (in column 1 of the second array)
    - else, if it contains "SITE" get the X, Y and Bin values (in columns 2, 3 and 4 of the second array)
    - else, save the values in the other columns of the array (redim as required)

    - Finally, copy to output sheet. First Array#2 then Array#1 (resized to rows number of array#2)
    - if the final array has more then 65000 rows, split in multiple sheets

    Other option :
    - use a Custom Classes
    - but this still requires to read line by line and make a decision base on the value read.

    Not sure if this would work... who knows...
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Most efficient way is to read / write an entire file once :

    Please Login or Register  to view this content.
    The second array DR is dynamic, so no need to count the number of <EOS> values to determine its size …
    Last edited by Marc L; 12-14-2013 at 10:40 AM.

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Efficient way to read in text file and account for possibility of large number of rows

    @Marc L
    Nice code!

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    marcL,

    thanks.......i am trying to run your code but i am having an issue just getting the dir/path setup.

    I put in the dir and path and name of the file but it just beeps at me......i know i am doing something wrong but not sure what .........i will keep looking at it but thought i would ask

    "D:\Tests4Noobs\welchs101 test file .txt",

    what part of the above is just the path and what part is the file name..........

    thanks again.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Red face Re: Efficient way to read in text file and account for possibility of large number of rows

    Quote Originally Posted by GC Excel View Post
    @Marc L
    Nice code!

    Nice and easy, what else ?‼

    Merci, tabernacle !


    Quote Originally Posted by welchs101 View Post
    I put in the dir and path and name of the file but it just beeps at me ...

    what part of the above is just the path and what part is the file name ...

    Beep means the file doesn't exist ! …

    File starts after the last backslash …
    Last edited by Marc L; 12-14-2013 at 11:54 AM.

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    i am starting to think that maybe i am looking at this wrong

    Please Login or Register  to view this content.
    i thought TXT$ was a variable but it does not seem to be referenced anywhere else

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    for some reason i am not getting past this point

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Efficient way to read in text file and account for possibility of large number of rows


    Beep means the file doesn't exist ! …

    File starts after the last backslash …

    Wait a little, I'll do a mod to select the file to open …
    Last edited by Marc L; 12-14-2013 at 11:53 AM.

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    i have the dir/path in there......and the file name is "test_input_file.txt" but it does not seem to be working.......any idea what i am doing wrong

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Efficient way to read in text file and account for possibility of large number of rows


    You just don't well assign the file ‼

    Wait a little, I'll do a mod to select the file to open …

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Mod procedure to choose the txt file to convert :

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    hi marc.........wow it worked!

    however,the output does not contain the header information. also the cols A through O should be information from the informatin from the header section of the input file..........

    but thanks a lot ..........this gives me a great start on how to do this.......just need to figure out how to get the headers in there.........see my output file in original post for what i am talking about.........

    merci

  14. #14
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    marcL, i am going through your code......very nice. I will have more questions but my first is this

    what is this variable

    HF%

  15. #15
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    i guess i dont understasnd this

    Please Login or Register  to view this content.

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Last but not least …

    Quote Originally Posted by welchs101 View Post
    see my output file in original post for what i am talking about ...

    Yes I saw that : it's one of the most awful import / export file structure I've ever seen ‼

    'cause the thread purpose is upon the "Efficient way to read in text file" and after reading the GC Excel's answer,
    I wanna just demonstrate a faster way and to bring you a good code basis as well …


    Quote Originally Posted by welchs101 View Post
    i guess i dont understasnd this
    Please Login or Register  to view this content.

    'cause of the awful structure, this is my last step forward code :
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    this is fast ......very fast.......thanks.......i will try it out on a large number of rows but i guess it will be just fine. i am going to learn some as i go through this code in more detail.

    i just have to know figure out how to get the headers in the file. but thanks alot!!!!!!!!!!!!

  18. #18
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    again i just wanted to say thanks........i really like the way you assign all the rows in the input file to an array and then manipulate the array to get the output.

    the file structure is not something i can change. the input file structure is fixed by the program that collects the data. I just have to output a file structure so that we can analyze the data..........

    thanks again!!!!!!!!!!!!!!!!

  19. #19
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    does anyone know what this is doing

    Please Login or Register  to view this content.

  20. #20
    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: Efficient way to read in text file and account for possibility of large number of rows

    Hello Welchs101,

    It is checking the string "V" to see if the characters in positions 1 to 5, starting on the left side, equal the string "Site_". If true the variable N% will be True or -1, If not it will be False or 0.
    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!)

  21. #21
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    thanks.........for some reason i was just not seeing that....thanks that makes sense

  22. #22
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    just curious why it equals -1 for true instead of 1

  23. #23
    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: Efficient way to read in text file and account for possibility of large number of rows

    Hello Welchs101,

    You're welcome. The use of double equal signs looks wrong. Perhaps it would have been better to write it this way...
    Please Login or Register  to view this content.

  24. #24
    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: Efficient way to read in text file and account for possibility of large number of rows

    Hello welchs101,

    In VBA, True is represented by -1 (all bits in the word set). Other programming languages often assume any value other than zero to represent a Boolean True state.

  25. #25
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    thanks......

    ok, i have been going through this code learning a lot........but i have to admit i am not sure i know the best way to somehow incorporate the "headers" .........anyone have a suggestion?

  26. #26
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    If you want the header identical to what you have, it needs to be modified further.
    See if this is good enough.
    Please Login or Register  to view this content.

  27. #27
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    i am just amazed at the quality of programming you guys do..........more still to learn............

    i will review this as well. thanks.

  28. #28
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    Forgot to add one line
    Please Login or Register  to view this content.
    To delete unnecessary comma after the last column.

  29. #29
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    jindon, i am trying to figure out where in your code you reference the headers........i see the text "waferid" but i dont see the text for example "HdrOperatorID"......where is this in the code

  30. #30
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    In one of
    Please Login or Register  to view this content.

  31. #31
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    thanks. i dont know why i did not see that the first time......guess i was tired.

  32. #32
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    does this

    Please Login or Register  to view this content.
    read the ENTIRE file into the variable "txt"?

  33. #33
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    Correct.

    1) read the file
    2) generate header + prefix for each new records from beginning part up to <EOH>.
    3) add "Wafer_ID" to header + prefix from the line contains "LotID".

    2) + 3) for static header + prefix to each recods.

    4) new record will be determined to find the line starts form "Site".
    5) get header for "SiteX", "SiteY" and "BIN", then allocate corresponding value.
    6) Else, use 1st part split by a comma to get header, and 2nd part for data.

  34. #34
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    thanks jindon......as always many many thanks.

    I did have another question either for you or someone else if they can answer it:

    What is going on here in this section of code.....why did you do this I am trying to figure it out....
    Please Login or Register  to view this content.

  35. #35
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    ok...I think I understand.......your basically just copying array "a" contents to new array "temp"...........but did you use application.index is there some advantage to use thing .........also are there limitations to using this?

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    It is for CSV out put.

    "a" is a 2D array and can be out put in a cells, but not to csv file as it is.

    Need to convert it to a single D array to use with Join function later, so those lines are converting each row of array "a" to temp joining each element with a comma.

    Do a step debug, so that you will see what is going on.

  37. #37
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    when I have a large file......ie...many rows of data in the text file........

    I get a runtime error at the following location:

    Please Login or Register  to view this content.
    Is there a limitation on the "Application.Index"......ie....will it not work for "large" arrays?

  38. #38
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    try change that line to
    Please Login or Register  to view this content.

  39. #39
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    that fixed it!


    Just curious though........is there a "limit" to the application.index function? or should I submit another post for this question?

    thanks.

  40. #40
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    Yep, it has a limit that is 2 ^ 16.

    So, it will not work when Col/Row has more than 65536.

  41. #41
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    jindon,

    in the last two weeks I have run into situations where vba-excel type function have 2^16 limitations........is there a list of vba-excel type functions with this limitation so I can know ahead of time.........
    thanks.

  42. #42
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    I don't know about the documentation for the limit.

    Known limit for me is Transpose/Index functions, try not to use them and simple loop is faster than using such functions for a bulky data.

  43. #43
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    thanks for the info. I am starting to get a sense of "don't use excels built in worksheet type functions" for large data sets.

    I did have another question:

    Consider the following code:

    Please Login or Register  to view this content.
    this creates an array of values. However, the first element in the array is nothing......

    Is there a way to get the first element in the array to be the first line after the "<EOH>"

  44. #44
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Efficient way to read in text file and account for possibility of large number of rows

    maybe so
    Please Login or Register  to view this content.

  45. #45
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Efficient way to read in text file and account for possibility of large number of rows

    Please Login or Register  to view this content.

  46. #46
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Efficient way to read in text file and account for possibility of large number of rows

    jindon, that worked.


    nilem, I actually tried the one you suggested before I made the post because I thought that should work........but for some reason it did not......I guess that space in the string is perhaps not really a space........

+ 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] Organize large space delimited text file into appropriate rows and columns in excel.
    By rdlaner in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-29-2013, 02:25 PM
  2. Import large Hex File and be able to input number of rows and columns to use
    By ssaverin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2013, 11:03 AM
  3. Read text file to ListBox and Delete rows in ListBox and re-write text file
    By Nu2Java in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-29-2012, 09:42 AM
  4. Efficient way to animate changing colors for a large number of shapes?
    By nannerdw in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2011, 08:08 AM
  5. Large Form Help: Handling large number of text boxes and labels
    By laterdaysluke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2010, 03:12 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