+ Reply to Thread
Results 1 to 20 of 20

Store text file in an array

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Store text file in an array

    Hello everyone
    I have text file and I need to store it in an array
    The lines needed to be stored is after this line
    Please Login or Register  to view this content.
    till before the last line
    Please Login or Register  to view this content.
    Then to sort the array based on the column of total in descending way ..
    Note : I don't need to open the text file as excel file .. I just need to deal with the text file directly
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Store text file in an array

    Hello YasserKhalil,

    You may find some good guidelines here: https://www.thespreadsheetguru.com/b...ide-text-files

    Hope you find it useful.

    Kind Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: Store text file in an array

    Try this:
    (I'm sure it's possible to sort in VBA, but easier in a worksheet!)
    Please Login or Register  to view this content.

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    Maybe :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    Thanks a lot for all of you .. You are really awesome
    @mgs : your solution is perfect but in fact I will deal with about 2000 text files with the same structure so creating temp sheets I think will take longer time so you noticed that I told in first post that I want to deal with the text files directly

    @karedog your solution is awesome ..if possible I need three points :
    First: I want to skip the line that start with AVG
    Second : After seeing the results I found that some totals are the same so in this case I want to sort the first column in descending
    order
    Third : I need to get rid of the characters (* and +) from the numbers

    Thank you very very much for great help
    Last edited by YasserKhalil; 03-22-2017 at 02:39 AM.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    You are welcome Mr.Yasser Khalil, thanks for rep.points.
    Please Login or Register  to view this content.
    Last edited by karedog; 03-22-2017 at 03:05 AM.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    Awesome Awesome Awesome Mr. karedog
    That's exactly what I was seeking for
    Thank you very very much for incredible and wonderful help
    Best Regards

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    You are welcome Mr.Yasser Khalil, glad I can help.

    Regards

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    I am so sorry for disturbing you my friend
    I have two empty rows in results in this sample ..
    If there is a fix just point it to me please
    Attached Files Attached Files

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    In case the blanks must be located at the end :
    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    Now it is perfect .. Thanks a lot for great and continuous help
    Best and Kind Regards

  12. #12
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    You are welcome Mr.Yasser Khalil.

    Regards

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Store text file in an array

    Hi,

    Here's an ADO option, which I wrote mostly to refresh my own memory!
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    Thanks a lot Mr. xlnitwit for your great contribution

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    @Mr. Karedog
    I have this sample text file that I tried to do the same but with column named "Hits"
    Tried this line instead
    Please Login or Register  to view this content.
    But it doesn't do the trick
    Can you explain what should I edit or fix in the code ..?
    Please don't edit the whole code .. just point me out the changes if possible

    In this sample column Hits will be as Total from previous sample ....so I need to sort this "Hits" column in descending order ...
    the same strategy except for the header of the column and the column number
    In previous sample: the header was "Total" and it was column number 7
    In this sample : the header is "Hits" and the column number is 12

    Thanks a lot for great help
    Attached Files Attached Files
    Last edited by YasserKhalil; 03-22-2017 at 09:03 AM.

  16. #16
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    The purpose of this line :
    Split(Input$(LOF(1), 1), "Total" & vbCrLf)(1)
    is to discard anything left to "Total" in the whole string, in other words, to discard the first two lines of data of file Sample.txt.

    Lets take a look at this code :
    Please Login or Register  to view this content.
    As you can see, if str1 is splitted using "jumped ", the result of Split(...)(0) is the left part, while Split(...)(1) is the right part.

    The same thing applied to previous Sample.txt, the first three lines are :
    "Test Draw 5811, Thu 05/01/2014","Test Draw 5811, Thu 05/01/2014","Test Draw 5811, Thu 05/01/2014","Test Draw 5811, Thu 05/01/2014","Test Draw 5811, Thu 05/01/2014","Test Draw 5811, Thu 05/01/2014","Test Draw 5811, Thu 05/01/2014"
    ##,#02,#09,#10,#20,#36,Total
    06,12,16,8,15,16,67
    so if we split using Split(str1, "Total" & vbCrLf)(1), the result is the right part after "Total" & vbCrLf, in other words, we take the string start from line 3 and afterwards.

    So "Total" here is the 'end string' of the line to be discarded, not the string of the column we want to sort.

    In your new Sample.txt file, the string that acts as delimiter to discard unwanted left part is "Hit Ratio" (there is a double quote) at the end :
    ##,Out,Out/Med,Longest,L10,L5,Last,Avg,Med,"Std Dev","Exp Hits",Hits,"Hit Ratio"
    01,29,5.8,51,0,0,3,8.2,5.0,8.6,231.2,204,88.2%
    so the new code should be :
    Please Login or Register  to view this content.


    On your first Sample.txt, the last line is : AVG,12.7,12.9,13.9,14.9,14.8,69.1
    while on your last Sample.txt, the last line is : Avg,8.44,0.04,44.56,1.22,0.61,6.32,8.00,4.77,7.63,231.2,231.2,100.0%

    Notice that the first one the "AVG" is all capital letters, while the latter is not, so this line code must be changed to :
    Please Login or Register  to view this content.


    In previous Sample.txt, you want to sort based on 7th column, while on new file, you want to sort based on 12th column so :
    Please Login or Register  to view this content.


    Complete code :
    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    OMG Thank you very very much for that masterpiece explanation. That's wonderful and I liked it a lot

    Can this wonderful code be converted to public procedure .. so as to be easier to use with many structures ...?

    I imagine
    Sub GetFromTextAndSort(strPath as string,colNumber as integer,sep as String,Optional colToExclude as integer)

    And use it in this way
    GetFromTextAndSort ThisWorkbook.Path & "\Sample.txt",12,"Hit Ratio""",13

    Best Regards

  18. #18
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    I have tried this
    Please Login or Register  to view this content.
    But couldn't figure how to use c12 in the parameters of the public procedure
    Last edited by YasserKhalil; 03-22-2017 at 01:07 PM.

  19. #19
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Store text file in an array

    Maybe :
    Please Login or Register  to view this content.

  20. #20
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Store text file in an array

    OMG That's the most wonderful code I have ever seen. It helped me to deal with about 4000 text files in less than a minute
    Thank you very very much Mr. karedog for this masterpiece
    Best Regards

+ 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] Check for text files in a path and store the file names in a array of string
    By balajinatarajan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2016, 02:54 PM
  2. [SOLVED] Dynamic Changing Data, Based on Criteria Store in Array, Sum array and store in Cell
    By penbeacho in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2015, 10:31 AM
  3. [SOLVED] VBA: Pull unique text records from multiple columns and store into array?
    By kaptenstofil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-24-2014, 10:39 AM
  4. Store array formula content in a VBA array
    By cgkmal in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2013, 02:24 PM
  5. Replies: 4
    Last Post: 05-02-2013, 11:16 AM
  6. Replies: 25
    Last Post: 01-24-2013, 06:07 AM
  7. macro to store each ine of a text file in an array
    By mithil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2010, 01:41 AM

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