+ Reply to Thread
Results 1 to 10 of 10

For Loop Code is Very Slow - How can I speed it up?

  1. #1
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    For Loop Code is Very Slow - How can I speed it up?

    Hi all,

    the below code works but is VERY slow. I am sure it has to with a for each...next inside a for loop...next.

    Is there a better (and faster) way this can be set out?

    Please Login or Register  to view this content.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: For Loop Code is Very Slow - How can I speed it up?

    Why do you need the outer loop anyway ?

    You just repeat the same code over and over again.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: For Loop Code is Very Slow - How can I speed it up?

    the outer loop is required as the conditions change when the iteration occurs. Should the outer loop be removed then?

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: For Loop Code is Very Slow - How can I speed it up?

    I don't know what your data looks like and what you're trying to achieve,

    but assuming you have 1000 rows of data you then loop a thousand times 1000 rows of data. You can imagine why the code is SLOW.

  5. #5
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: For Loop Code is Very Slow - How can I speed it up?

    Oh ok, then that makes sense as to why its so slow. I will post a sample

    I have attached a sample and I have had to de-sensitive the data.

    Essentially, the procedure goes through a series as shown in col U:V. This number is the divisor in col K. If the bank is bigger than the start bank, then the series starts again from 20. If not, it goes to the next number.

    I'm hoping there is a more simpler way to achieve this?
    Attached Files Attached Files

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: For Loop Code is Very Slow - How can I speed it up?

    If I run the code on these numbers all is set after the first outer loop. After that nothing changes anymore with each outer loop

    So I still don't see the purpose of the outer loop.

  7. #7
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: For Loop Code is Very Slow - How can I speed it up?

    got it, thank you. I removed the outer loop and it seems faster. I will do some more testing.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: For Loop Code is Very Slow - How can I speed it up?

    Some tips for improving loops
    1. Any actions that can be done outside the loop, keep them outside the loop
    2. If manipulating worksheets, turn off events & screenupdating before the loop (Turn on again after) (You could also try changing the Calculation method but it shouldn't be necessary after doing these two)
    3. If manipulating excel ranges, look into if you can achieve your desired outcome using arrays instead. If you can, you will speed up your code by a few orders of magnitude. It is much faster to read the range into an array, manipulate the data inside the array and paste it back when done. I have achieved improvements of up to 60x faster.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  9. #9
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: For Loop Code is Very Slow - How can I speed it up?

    This seemed to run a little faster on your small sample...
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: For Loop Code is Very Slow - How can I speed it up?

    thank you! That also works quite well.

    However, just one further question. I seem to get a number in col K in the next blank row. I have tried to remove it but nothing seems to work?

+ 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. [SOLVED] Optimize Slow VBA Code, need for speed :)
    By antonio32 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-28-2017, 06:50 AM
  2. Need for speed , slow vba code
    By antonio32 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-27-2017, 10:54 AM
  3. Slow VBA Code - Help Speed Up
    By CC268 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-26-2017, 10:45 AM
  4. Problems with slow code, can this be coded differently to speed it up.
    By molesy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2014, 07:01 AM
  5. Username Code help - Slow processing speed
    By JBurton1986 in forum Excel General
    Replies: 6
    Last Post: 02-21-2014, 06:53 AM
  6. [SOLVED] Slow VBA Code for simple Loop and add pasted values Any Way to Speed Up
    By John Vieren in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-10-2013, 11:16 AM
  7. [SOLVED] Speed up slow macro loop
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-15-2012, 11:41 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