+ Reply to Thread
Results 1 to 20 of 20

Read .TXT into Array in VBA

  1. #1
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Read .TXT into Array in VBA

    Hey everyone,

    I have a text file that I'm bringing into VBA with the FileSystemObject method.. I want to read the data that is in the .txt file into an array. In the text file there are 5470 lines and 20 columns.. putting a "watch" on my "arr" variable, I basically want the first row to be 20 categories, and then every row thereafter to be sorted into those 20 categories.. This is my code this far.

    Please Login or Register  to view this content.
    I have also attached an image of my "watch" - Ideally I would like the values of arr(0)(0) through arr (0)(20) to be the expressions themselves, and then every one of the 20 categories in arr(1) through arr(5470) to fall into their respective categories..

    Arr.PNG

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

    Re: Read .TXT into Array in VBA

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

  3. #3
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    Hi jindon, thanks for the quick response!

    VBE gives me a "Runtime Error '13': Type Mismatch" error at the below area in the code.. not sure why, I am running 2003 if that makes any difference.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Read .TXT into Array in VBA

    It would be beneficial to see a sample text file (even with dummy data) and a workbook showing what you expect the results to be. From your brief description, I would do something like this:

    Please Login or Register  to view this content.
    I'm sure it's off, but it should give you some ideas.

    abousetta

    ---------- Post added at 08:01 AM ---------- Previous post was at 08:00 AM ----------

    What is you delimiter? Both @jindon and I put a comma as this a common delimiter but if it something different then you need to change this.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    Hey abousetta, thanks for the reply, will look into your code.

    Attached is a text file with several lines of dummy data. Ideally what I am trying to do is create a user form in which the user can enter a date range, PV01 range, etc etc, and have the spreadsheet return the corresponding results from the array.. without actually having to physically read the data into the spreadsheet, as I want to keep the size of the spreadsheet small.

    I'm using vbTab as my delimiter. -- unfortunately, your code also gives me a "Runtime error '13': Type mismatch" error, at the line below.

    Please Login or Register  to view this content.
    Sample.txt
    Last edited by jasperhuang93; 07-24-2012 at 11:08 AM.

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

    Re: Read .TXT into Array in VBA

    try
    Please Login or Register  to view this content.

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Read .TXT into Array in VBA

    Try this... I will check back later to see if it works or not as I only briefly tested it:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    @jindon, your code works perfectly for outputting the text file into the workbook.. though is there a way to create this sort of array "in the cloud" so to speak, so that I can create a userform that references the array for specific data? If not I could always just have to reference a sheet in the book.. I'm just afraid of data bottlenecks as the amount of data in the file becomes greater.

    @abousetta, when I put a watch on "arrFinal," it more or less mirrors what the watch on "arr" does in my original code.. is there something I'm missing?

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Read .TXT into Array in VBA

    Hasn't this really been answered here: http://www.excelforum.com/excel-prog...txt-file.html?

    Must admit I'm curious why you don't use a database since you expect this data to grow consistently, as it does, these methods will get slower and slower and you'd be better off with a database/ADO queries from the other thread

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

    Re: Read .TXT into Array in VBA

    Like this?

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    It's mostly because I have very little experience with any type of databasing.. so I'm averse to trying it, if I can avoid it.. but I guess it's inevitable. Would you recommend Access?

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Read .TXT into Array in VBA

    Do you have it installed? If so, try it, it's the most user friendly.

    If not, then probably sql server express (but install the management studio also - makes things much easier), because it's free and will handle a lot of people connected to it at the same time.

    It'll be a learning curve, but well worth it

    To interact with excel you've already got 90% of it from Joseph's post on the other thread

    How many people do you envisage using your workbook at any one time?

  13. #13
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    Access is install, I'd likely have to request permission to get SQL Server Express.. Access could probably do the trick, I only expect at most 10 individuals to be accessing the data.

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Read .TXT into Array in VBA

    Give access a whirl then It's much more suited and if you all have access installed, then dump Excel altogether

  15. #15
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    Unfortunately I don't think dropping excel would be an option currently.. This is one part of a larger project. -- In a nutshell, particular bonds need to be called when a user inputs his/her criteria. Depending on what the inputted criteria is, there is also code written to query internal databases for rates and other information. Specific math is then run on the Par value, DV01, PV01, and the retreived rates etc etc to produce the "hedge rate."

    The model is, in simple terms, a dummy swap pricer, which allows then end user to figure out whether the swap is too risky, or okay to put on, and this part of it is simply the component to allow the user to pull bond information based on criteria he or she inputs.

  16. #16
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Read .TXT into Array in VBA

    Ah right, fair enough.
    there is also code written to query internal databases for rates
    Can't you also query the same databases to pull bond info rather than trying to use a text file?

  17. #17
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    The necessary bond information is dumped into the text file every other day. Apparently I don't have the necessary permissions to access the database because it stores trade inventories not only for municipal bonds, but all products.. to be honest seems like a bunch of BS because it's not as if I would ever query for anything else.. my plate is full enough already. But this is why I am trying to do this

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Read .TXT into Array in VBA

    Ah right, how many lines are likely to get added to the text file?

  19. #19
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Read .TXT into Array in VBA

    You can:

    - change the extension of the file into .csv and open it as a workbook.
    - replace the Tabs in teh txt-file so it can be read as a scv-file: open it as a workbook
    - read the split content of the file into column A and distribute the data, using texttocolumns

    Please Login or Register  to view this content.



  20. #20
    Registered User
    Join Date
    07-18-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Read .TXT into Array in VBA

    Quote Originally Posted by Kyle123 View Post
    Ah right, how many lines are likely to get added to the text file?
    I'd like to say because it is an inventory of municipal bonds, it can technically grow to the number of municipalities in the US (roughly 30k), though I highly doubt that, and a lot of them don't even issue paper.. so I'd say it could grow to 10k, give or take.

+ 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