+ Reply to Thread
Results 1 to 13 of 13

Reading in text file into array. Each element is a line of file. Why last element a space

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

    Reading in text file into array. Each element is a line of file. Why last element a space

    Hi,

    i am providing macro and test input file. Note, please change ext of input file from .txt to .sat.

    If you run the macro and stop it just after reading in the file then yo uwill see that the last entry in array A(16) is a space .........why? and how to prevfent this?
    Attached Files Attached Files

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    It is not space - it is null string (""). Your file ends with vbNewLine and because you split on this character(s) the array has an empty element at the end. Why does this make a difference?
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    You split using vbnewLine. So it returns an empty element an the end of your array.
    It splits just after 10.46

    You can Redim your array if you don't want the last element
    Please Login or Register  to view this content.
    GC Excel

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

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    ok.....so if i use vbnewline the last element in my array of lines from file will always be a nullstring?

    if not then i what i prefer to do is have the last element in the array be the last actual line of data.

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    The data in your file ends with a new line - if you go to end of the file you are on blank line, not the last line of data. If you do not wish this, your following code may test for "" as array element or you may ignore this line at the start:
    Please Login or Register  to view this content.

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    did my last post make sense? typed it kinda fast.

    If i simply remove/ignore the last element in the array (which i can do) will there ever be a time where removing/ignoring this last element causes me to remove/ignore actual data? if so then wouldnt it be better to some how read in file so that the last element in the array is the last actual row of data.........

    of course how to do this i dont know

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    thanks Izandol

    i was hoping you could explain these lines of code a little more

    Please Login or Register  to view this content.
    i think the first line assigns the entire text-file to the string variable stFile.
    But i dont get the next two lines..........

  8. #8
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    That is correct. The first line reads in the whole file. The second line tests if it ends with vbNewLine and removes that if it does so you will not have an empty element when you Split.

    Note that vbNewLine is actually two characters - a carriage return (vbCr) and then Line feed (vbLf) so we must test last two characters and not one.

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    thats exactly what i needed to know to understand it thanks.

    i did not realize the vbnewline was two characters......

    thanks again.

    oh, one question i did have is this:
    Reading in the entire file into a single string variable.................is there a limit to the size of the input file that can be assigned to the singl-string variable..........i guess does string variable have a num-char limit?

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    String variable limit is near 2 billion characters. If you have so much data I think you should use a different method!

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    wow! good to know..........2billion............wow!

    thanks for the info

  12. #12
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    You may also like to consider ADO to read the text file data into a Recordset. It depends very much what you do with this data and how many records there may really be.

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

    Re: Reading in text file into array. Each element is a line of file. Why last element a s

    hi,

    never used ado......or at least i dont think i have.........something new to learn. any good info on this ado?

+ 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] Stat table with formulas for each data element but only when the element exists in table
    By stevemills04 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2013, 05:50 AM
  2. [SOLVED] STart reading text file at line 2
    By Striker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2006, 08:35 AM
  3. Reading a text file line by line-How do I open the text file with VBA
    By stressman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2005, 12:29 AM
  4. [SOLVED] read data element from XML file
    By Steve Moreno in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2005, 06:05 PM
  5. Reading a text file line by line
    By Foss in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2005, 12:06 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