+ Reply to Thread
Results 1 to 4 of 4

Help optimizing this code

  1. #1
    Registered User
    Join Date
    08-01-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    5

    Post Help optimizing this code

    Hello,

    This code opens a new workbook, loops throughout all excel workbooks in a folder, opens the first one, loops throughout the cells of the table of the first worksheet and if the conditional formatting color is the desired, copies the cell value from the first column which is an ID to the new workbook.

    It's basically so I can know what IDs are still undone in all the files.

    It works but as more files are added to the folder, the slower it gets (at the beginning I call another subprocedure to turn things off like events, screen updating and so on which I ommited here). It's still faster than checking manually each file but maybe it can be optimized somehow. I thought of trying with an array but don't know exactly how I could do it.

    Please Login or Register  to view this content.
    Last edited by mike-lowski; 08-02-2021 at 08:43 AM.

  2. #2
    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: Help optimizing this code

    This question really should be a FAQ. I see it time and time again and looping just isn't the best way of doing this sort of thing.

    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.

    I also think it's risky to use colours to identify cells, there are far too many. There's presumably some other marker, like the absence of a value in a cell that could be used to Autofilter the rows/range you need.
    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.

  3. #3
    Registered User
    Join Date
    08-01-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    5

    Re: Help optimizing this code

    Thanks Richard. Indeed looping is hell most of the time.

  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: Help optimizing this code

    Quote Originally Posted by mike-lowski View Post
    Thanks Richard. Indeed looping is hell most of the time.
    Indeed, and if you need to use a loop because things are slow then read all the data into a VBA array and process the array with the loop saving results into a Results Array and at the end copy the Results array back to the desired Excel range.

+ 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. Need Help Optimizing Code
    By joey2point0 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-25-2017, 03:29 PM
  2. Optimizing Code
    By tucanj in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2013, 11:08 PM
  3. Optimizing Code
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2012, 11:20 AM
  4. Optimizing the code
    By kmlprtsngh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-10-2010, 01:27 AM
  5. Optimizing the Vlookup code in VB. Repitive code with different referencing range.
    By raknahs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-23-2010, 05:03 PM
  6. Optimizing code
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-06-2009, 11:40 PM
  7. Optimizing Code
    By Jim Thomlinson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-02-2005, 10:11 PM

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