+ Reply to Thread
Results 1 to 10 of 10

VBA to read text file fast and efficient

  1. #1
    Registered User
    Join Date
    02-21-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    40

    Talking VBA to read text file fast and efficient

    Hi,

    I have the below vba code which reads in a tab delimited text file fine. The issue however is the length of time it takes as the file is in excess of 500mb. Basically in this example I only want to bring back the records where the first column is in range B5. Is there a slick way this can run much faster?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA to read text file fast and efficient

    Can you import the TEXT file, then filter on whatever data you need ONCE then copy and paste?
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to read text file fast and efficient

    Since it's tab delimited, I would use VBA to open it in Excel instead of reading it as a text file. Without having the file I can't do a test for you or confirm performance improvements but it would look something like this:

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    02-21-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: VBA to read text file fast and efficient

    Thanks Jeff
    Does the maximum number of records in excel pose a problem when opening the file via excel?
    Basically I type a reference number in cell B5 on sheet1, click the macro then I want to it put those records from cell A11 down on sheet1 from the tab delimited file.

    e.g. if cell b5 = "123" then I want to it bring back all records from the tab delimited file & insert from row A11 downwards on my spreadsheet. The original code I had did that however it took too long to read from a significantly big text file.

    The code above, when I click the macro it isn't bringing back any records when I know they are there.

  5. #5
    Registered User
    Join Date
    02-21-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: VBA to read text file fast and efficient

    The above code does seem to open the file much quicker however it is not outputting the records.
    Your help will be much appreciated.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to read text file fast and efficient

    I can't run this code without a sample file. Can you provide one? If the data is private just mock up a few rows of fake data.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to read text file fast and efficient

    Quote Originally Posted by DaveNUFC View Post
    Does the maximum number of records in excel pose a problem when opening the file via excel?
    Your version of Excel has a limit of about 65,000 rows. Later versions have a bigger limit of about 1,000,000 rows. I don't know what happens if you try to open a tab-delimited file that is over the row limit.

  8. #8
    Registered User
    Join Date
    02-21-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: VBA to read text file fast and efficient

    Cheers
    See attached, this is an example of a massively trimmed down version.
    Say for instance I wanted a macro which read in this file & only brought back records for reference 123- the first bit of code I posted would do that however on a huge file it takes considerably long to process line by line.

    I am after a more efficient bit of code. I think the one you posted will truncate at the maximum number of excel records when the file becomes in excess of the max in excel
    Attached Files Attached Files

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to read text file fast and efficient

    I will take a look but if your files exceed the row limit in Excel then my solution is not the right one for you. But I don't know a way to make text file I/O any more efficient. My thought would be to use something besides Excel.

    I'll get back to you tomorrow.

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

    Re: VBA to read text file fast and efficient

    This creates new text file with the rows that is specified in Sheet1!B5 and read it to Excel.
    It is working on your uploaded text file.
    Not sure if this makes it faster though, but give it a try...
    Please Login or Register  to view this content.

+ 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. Importing text file. Does entire file get read into memory first?
    By sumdumgai in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2016, 07:28 PM
  2. Replies: 45
    Last Post: 12-19-2013, 10:17 AM
  3. A fast way to open a workbook as read-only
    By Raymond85 in forum Excel General
    Replies: 11
    Last Post: 01-13-2013, 06:52 PM
  4. Replies: 0
    Last Post: 10-31-2012, 09:59 AM
  5. 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
  6. Read whole file,Match the headers based on specific condition & write to text file
    By macromaniac in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-07-2011, 06:37 AM
  7. read value of strings from a text file and make an excel file.
    By gashforing in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2009, 07:12 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