+ Reply to Thread
Results 1 to 7 of 7

Slow Execution of a Loop

  1. #1
    Registered User
    Join Date
    07-23-2013
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    87

    Slow Execution of a Loop

    Hi guys,

    I have built a user form that enables product search, I have Googled and found a simple enough form and apply like for like to my sheet. The problem is, it runs very very slow on my workbook. Initially, my thought was the number of products in the stock list, so I mirrored the product table into the sample file. It makes no real difference in terms of speed for the sample file to return the results instantly (257 rows of products) but mine is still extremely slow!

    Below is the original code from the sample file which runs lightning speed even without any of the disabling event, screen updating etc.

    Please Login or Register  to view this content.
    This is my code

    Please Login or Register  to view this content.
    How much slower is based on how many results it returns. So if I search for a product that is unique, the loop takes around 0.64 sec, if I search keywords that returns 5 results, the loop will take 3.27 seconds. Compare to the template I have downloaded, which only takes less than a second regardless of how many results returned. I know my worksheet is bigger compared to the sample excel file but as I have disabled all events screen updating and formula updates, why it take so long to run that piece of code. Your help is greatly appreciated!

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Slow Execution of a Loop

    It's not the prettiest code but I don't see anything *obvious* here that would cause a dramatic slow down. Any chance you can post of copy of your workbook with any sensitive data redacted?

    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: Slow Execution of a Loop

    As WideBoy states above it is difficult to do anything without a working sample file. When i looked at the code the only thing i can spot that will slow it down is the amount of times it writes to the spreadsheet.

    It is far fromperfect but the below is (roughly) where you could go to with it, storing the values in a variant and then write the array to the destinatiion sheet at the end.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-23-2013
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    87

    Re: Slow Execution of a Loop

    Quote Originally Posted by CheeseSandwich View Post
    As WideBoy states above it is difficult to do anything without a working sample file. When i looked at the code the only thing i can spot that will slow it down is the amount of times it writes to the spreadsheet.

    It is far fromperfect but the below is (roughly) where you could go to with it, storing the values in a variant and then write the array to the destinatiion sheet at the end.

    Please Login or Register  to view this content.
    Hi CheeseSandwich,

    Thank you so much for amending the code. I run the search and a runtime error 9 occurred and highlighted the below line

    Please Login or Register  to view this content.

  5. #5
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: Slow Execution of a Loop

    Create a working sample file - upload and we can look further into optimisation. The error above is probably due to writing too many values into the fixed size array (i also get my x and y mixed up sometimes when writing to arrays)...

    The code created was more of a concept, it will fail if you pass it more than 99 rows of data. Until we see the actual data we won't know how to define the end of the data.

    A sample file that takes x time to run would be great as we would have something to benchmark from.
    Last edited by CheeseSandwich; 01-21-2022 at 07:45 AM.

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Slow Execution of a Loop

    Perhaps using the classes rather than worksheets could slow things up? Again, I'm just shooting in the dark here:

    Please Login or Register  to view this content.
    WBD

  7. #7
    Registered User
    Join Date
    07-23-2013
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    87

    Re: Slow Execution of a Loop

    Thank you WBD,

    I found the problem after deleting sheet by sheet to find out where is dragging it slow. I use a picture lookup which will put a picture into a cell base on another cell value. This requires a defined name and range which I have below.


    =INDEX('Sample Template'!$M$1:$M$2,MATCH('Sample Template'!$A$1,'Sample Template'!$L$1:$L$2,0))


    After deleting the above-defined name, the result is now almost instant. Not sure why this will cause the issue. I still wanted to use the picture lookup but am not sure how to remove the impact to my UserForm. Thank you!

+ 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. Cause and solution of slow execution of VBA!
    By meus in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 09-14-2015, 01:30 AM
  2. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  3. [SOLVED] Slow Macro Execution
    By Gandalf2524 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2013, 12:20 PM
  4. Slow VBA execution
    By GustavBA in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2013, 06:40 AM
  5. Slow Macro execution
    By rickparker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2012, 11:16 AM
  6. Slow SUMIF() execution?
    By ahartman in forum Excel General
    Replies: 6
    Last Post: 04-09-2010, 01:21 PM
  7. Slow code execution
    By side_ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-21-2005, 01:44 PM

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