+ Reply to Thread
Results 1 to 9 of 9

Nested loop hangs Excel

  1. #1
    Registered User
    Join Date
    07-04-2014
    Location
    slovenia
    MS-Off Ver
    2013
    Posts
    3

    Nested loop hangs Excel

    Hi,

    I have a problem with a nested loop in my macro as Excel hangs every time i run my script.
    I suspect it is because my sheets have too many elements.

    I am doing a software localization, and I need to translate records in column A (english) to column X (slovenian) in the same row. With each new version of the software, the number of rows changes.
    I already manually did one translation (44000 rows), and now i need to do a new version translation, so I want to use my old translation (because many if not most of the elements are the same) and insert values into the new excel file.
    In the new file in sheet1 I have a full column A with english words and an empty C column with slovenian words that I need to fill.
    Into sheet2 I copied the old english in column A and slovenian translation in column B.
    On sheet 3 i created a button that when clicked, goes though each row in sheet1.columnA, compares it to sheet2.colimnA, and if there is a match, copies the value from sheet2.columnB into sheet1.columnC.
    So, this should fill most of the translations I need to do, and the rest I will do manually

    I am quite new to VBA so this is what I came up with:

    Please Login or Register  to view this content.
    The number of rows is around 44000 in each sheet.
    Can someone advise me as to how i could optimize or even completely avoid the nested loop, because each time i click the button i need to wait for almost an hour and then Excel hangs.

    Thank you

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Nested loop hangs Excel

    If you have 44000 rows on each sheet you'll be looping 44000*44000 = 1,936,000,000 times.

    I think you should be looking at using VLOOKUP or INDEX/MATCH.

    For example this will look for the value from A1 in column A on Sheet2 and return the corresponding value from column B on sheet2.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: Nested loop hangs Excel

    Personally I would just use a formula (INDEX/MATCH) - copy it down - and then copy/paste special values to retain just the answers.

    Or an untested version of your code
    Please Login or Register  to view this content.
    Despite the high cost of living, it remains very popular.

    Don't forget to mark threads SOLVED when you get an answer and rep all the geniouses that helped you today!

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Nested loop hangs Excel

    hi ri051zhorzh, can you post a sample workbook (around 50-100 rows will be enough) and result expected on Sheet 3?

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Nested loop hangs Excel

    VB option, press Run button or run code "test" on Sheet1
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    07-04-2014
    Location
    slovenia
    MS-Off Ver
    2013
    Posts
    3

    Re: Nested loop hangs Excel

    Thank you for your quick replies, I am writing this from another computer and will post a sample work book in a few hours when i am back at my PC

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Nested loop hangs Excel

    you can test your data on the file provided in post #5

  8. #8
    Registered User
    Join Date
    07-04-2014
    Location
    slovenia
    MS-Off Ver
    2013
    Posts
    3

    Re: Nested loop hangs Excel

    Watersev thank you a million!
    I tried testing my data in the file you provided and it works perfectly!
    It finished all 44000 records in less than 2 seconds! I can not thank you enough!

  9. #9
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Nested loop hangs Excel

    you are welcome

+ 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. Noob programmer - without a 1 second pause in my VBA macro loop, excel hangs?!!!
    By HankMcSpank in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-17-2013, 07:35 PM
  2. [SOLVED] Macro hangs up in formatting loop
    By jacob@thepenpoint in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-09-2013, 08:52 AM
  3. Next without For error in nested loop - Escaping a Nested Loop?
    By BeneRich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-10-2012, 06:38 PM
  4. Excel hangs on DDE
    By zlackoff in forum Excel General
    Replies: 2
    Last Post: 07-12-2011, 09:39 AM
  5. For each loop nested; getting stuck on one cell in first iteration of nested loop
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-22-2009, 11:54 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