+ Reply to Thread
Results 1 to 15 of 15

Excel crashes while macro processes large text file

  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Excel crashes while macro processes large text file

    Hey,

    I have a macro which performs a search operation in a text file. The code accesses the text file (without actually opening it) and searches for a particular search text (provided by the user) by going through the entire text file line by line.

    The macro runs fine for a text file of size aound a 100KB. The problem comes when the user provides a file of 4MB or more. The excel hangs, or sometimes just plain crashes.

    The whole purpose of this macro is to search through large volumes of data (in a single text file). So when it crashes... well lets just say its a big problem.

    Is there any way to prevent this ?? Any help would be highly appreciated. I'm really lost on as to how to go about this problem....

    Also posted here
    Last edited by RitaG; 10-05-2012 at 10:35 AM.

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

    Re: Excel crashes while macro processes large text file

    And so am I if you do not explain which method you use to read the file.
    There's a plethora of methods in VBA.
    The 'line by line' approach doesn't sound very suited for your purpose.



  3. #3
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    The text files contains sections of lines, starting with "{" and ending with "-}".
    The user provides search criteria in the "Home" sheet. The user enters the search texts in a table in the "Home" sheet. (can be more than one search text).

    The macro needs to work like this : It has to locate those bunches of lines in the text file which starts with "{" and ends with "-}". Then the code has to check whether all the search texts are present in this set of lines. If yes, then it should be written to the sheet.If even one search text is not found then the entire set should not be written to the sheet.


    e.g we have a text file which has these lines :
    {12345678
    1:abc
    2:ddef
    3:xyz
    -}
    {12345689
    1:abc
    2:jklm
    3:ijk
    -}
    {98765543
    1:abc
    2:qwerty
    3:xyz
    -}

    the user enters the search criteira as
    search text 1-> 1:abc
    search text 2-> 3:xyz

    so the tool has to write the matching sets into a separate excel sheet. The following will get written

    {12345678 {98765543
    1:abc 1:abc
    2:ddef 2:qwerty
    3:xyz 3:xyz
    -} -}

  4. #4
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    And here is the code.

    Please Login or Register  to view this content.
    I hope now its a bit more clearer...

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

    Re: Excel crashes while macro processes large text file

    A 4MB text file is huge and opening it and looping through it line by line will always be slow and you're opening and parsing it for every record in your recordset that's mega slow - at least read it once and hold it in a variable, then look through that.

    Also, you shouldn't be using sql like that, there's a known bug when executing sql on the workbook it is called from - which won't really help things either.

  6. #6
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    I dont know how else to work this

    Any suggestions ??

  7. #7
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    And what should I hold in a variable ?

    If you meant the set of lines between "{" and "-}", then how do you store multiple lines into a variable ??
    Or if you meant each record of a recordet then again how do you do that ?

    I'm only just learning VB coding in excel so please bear with my ignorance...

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

    Re: Excel crashes while macro processes large text file

    You'd store them as a string, read them in once and same the results as a string, or even better as an array so you could filter it to extract the info that you're looking for.

    I wouldn't have called your code beginners

    I think, conceptually, I'd do it like this:

    I'd read each complete "object" into a single line string:
    {12345678
    1:abc
    2:ddef
    3:xyz
    -}
    Would become something like:
    {12345678;1:abc;2:ddef;3:xyz;-}

    I'd then save these single line strings into a single dimensional array.

    I'd then use the Filter() function on each search parameter to get each of the matching objects.

    I'd then compare the filtered arrays for objects that occur in both arrays, these would be the objects that match your search parameters

  9. #9
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    Wont there be a limit to how many characters one can put into a string variable ? What if the number of characters between "{" and "-}" exceed that ?

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

    Re: Excel crashes while macro processes large text file

    Well how many characters are between {}?

    I've only had the issue with string length once and that was on a txt file > 100MB - I tried to read the whole file in as a text string, so it's generally not an issue

  11. #11
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    okay... I'll have to try this.. and i have never worked with filters in VBA so I'l have to get back to you on this.
    Will this approach solve the problem of excel crashing while processing >4MB files?

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

    Re: Excel crashes while macro processes large text file

    At a guess it's not necessarily crashing, it is just taking so long to run the code it is unresponsive.

    This approach should speed things up, filters are very easy

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Excel crashes while macro processes large text file

    @RitaG,

    I'm afraid your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    HTH
    Regards, Jeff

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

    Re: Excel crashes while macro processes large text file

    Where's it posted Jeff?

  15. #15
    Registered User
    Join Date
    08-02-2012
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Excel crashes while macro processes large text file

    @jeffreybrown
    Sorry for the that ! I have edited my first post as suggested.

    @Kyle123
    Thank you so much !! I ran the 4MB text file and the code executed it in seconds ! Filters work like a dream. Thanks again

+ 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