+ Reply to Thread
Results 1 to 9 of 9

How to optimise counting in loops

  1. #1
    Registered User
    Join Date
    04-02-2015
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    47

    How to optimise counting in loops

    Hi, so I have a macro that takes an hour to process and I have an idea why but I do not know how to optimise it. If anyone can, can you please help?

    Thank you!

    Please Login or Register  to view this content.
    Last edited by Geode7; 05-14-2019 at 10:08 AM.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,210

    Re: How to optimise counting in loops

    Come on Goede7...Get with the program...lol

    By now you should know that an sample upload speaks a thousand words...
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    04-02-2015
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    47

    Re: How to optimise counting in loops

    It keeps failing to upload. Even though it is within the size limit.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to optimise counting in loops

    Difficult to know exactly what your macri is doing, but are you suer you shoud be looping a macro anyway. This subject really should be a FAQ. I see it time and time again and looping just isn't the best way of doing many of these things.

    Whenever you find yourself creating looping code stop and think is this really necessary? Most of the time it isn't. Loops are inherently slow when they have to jump back and forth between VBA and the Excel application - there's a big time overhead at each pass through the loop.

    The fastest, most efficient and elegant way for this sort of stuff is to use an Autofilter to filter the rows you're interested in.
    Then use syntax like

    Please Login or Register  to view this content.
    then paste to your destination cell.

    This uses just one Copy / Paste instruction rather than many if you're looping through many cells.

    You may need a heper column that contains a function which will identify the rows you're interested in so taht you can use the Autofilter. te macro could of course generate that helper column.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Registered User
    Join Date
    04-02-2015
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    47

    Re: How to optimise counting in loops

    Once I manage to figure out how to upload the unloadable file, then you will realise why.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to optimise counting in loops

    Quote Originally Posted by Geode7 View Post
    Once I manage to figure out how to upload the unloadable file, then you will realise why.
    What's the size of your file. We don't need zillions of rows a representative sample would do.
    The forum permits up to 9.6 Mb for .xlsb files rather than the ~1.2Mb for the rest


    To upload a file click the Go Advanced button at the foot of your post, look underneath the post area for the Manage Attachments section and take it from there.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: How to optimise counting in loops

    I'm not sure how many iteration you are performing at below section. But in general avoid use of Redim Preserve. Adding item to collection will be much faster than using Redim Preserve at each iteration.
    Please Login or Register  to view this content.
    Though difference wouldn't be significant unless you have 100k loop iteration or more (at 100k iteration, difference is about 1/2 sec) for simple loop.
    Also, consider using Instr() vba function rather than Application.Find.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  8. #8
    Registered User
    Join Date
    04-02-2015
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    47

    Re: How to optimise counting in loops

    Figured it out. Here is the sample.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-02-2015
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    47

    Re: How to optimise counting in loops

    So, I tried other macros and this works far faster. However it still takes 1 and a half hours to complete it. Is there a way to optimise this loop?

    Please Login or Register  to view this content.
    Last edited by Geode7; 05-15-2019 at 04:14 AM.

+ 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. Optimise using VBA (without solver)
    By djibbo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-17-2018, 01:34 PM
  2. [SOLVED] optimise trips
    By missticktock in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 10-21-2018, 12:02 AM
  3. [SOLVED] Loops & Counting w/ Conditions
    By DnDXan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-11-2013, 04:56 PM
  4. optimise calculations
    By canadave in forum Excel General
    Replies: 7
    Last Post: 02-02-2011, 01:27 PM
  5. how I can optimise this macro?
    By mithil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-29-2010, 03:42 AM
  6. How to optimise IFISERROR
    By AussieExcelUser in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-30-2006, 06:53 PM
  7. counting and loops
    By narutard in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 07-20-2005, 10:55 AM

Tags for this Thread

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