+ Reply to Thread
Results 1 to 7 of 7

Improve Speed: VBA Code very slow due to changing cell values in for loop

  1. #1
    Registered User
    Join Date
    01-13-2020
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2019
    Posts
    8

    Improve Speed: VBA Code very slow due to changing cell values in for loop

    Much as the title says. I have the following piece of VBA code:

    Please Login or Register  to view this content.
    This works 100% perfectly from a functionality point of view, but is very slow. I know enough about vba to understand that it is likely due to how often I am changing physical excel cells instead of dealing with a range, but not enough to no where to go with fixing it.

    I've attached an excel file that shows what it does. Macros 1-4 work. Macro 5 (the code included above) is the slow one, although it works.

    Functionality Wise, this is what I'm doing:

    Counting the rows (quick enough)
    Finding all the unique names and counting them (quick enough)
    Filtering by each unique name, and if the values of each unique name sum to 0, clearing the cells (this is slow)
    Tidying up by deleting all the unique names and all the rows I cleared.

    Any help appreciated
    Last edited by Nick_G; 02-03-2020 at 03:35 PM.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: Improve Speed: VBA Code very slow due to changing cell values in for loop

    You have
    Please Login or Register  to view this content.
    So for the first Loop that results in
    Please Login or Register  to view this content.
    On the next Loop, the result is
    Please Login or Register  to view this content.
    On the Loop after that
    Please Login or Register  to view this content.
    On the last Loop you'll get
    Please Login or Register  to view this content.
    Is that what you want?
    Or do you mean it to be
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: Improve Speed: VBA Code very slow due to changing cell values in for loop

    If you go out of your way and code it as inefficient as you can (many loops), it still is fast enough.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: Improve Speed: VBA Code very slow due to changing cell values in for loop

    Num_Unique_Codes should be
    Please Login or Register  to view this content.
    instead of the code you have now.
    The same for Numrows
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Improve Speed: VBA Code very slow due to changing cell values in for loop

    Quote Originally Posted by Nick_G View Post
    . . . I have the following piece of VBA code: . . .
    :
    Range("K5") = Application.Sum(Range("C20:C" & Numrows).SpecialCells(xlCellTypeVisible))
    :
    Range("A20:L" & Numrows).SpecialCells(xlCellTypeVisible).Clear
    :
    This works 100% perfectly from a functionality point of view, but is very slow. . . .
    The most likely way to speed up VBA macros which change cell contents, e.g., assigning values to cells, or clearing their contents, is to begin the macro with

    Dim acm As Variant
    acm = Application.Calculation
    Application.Calculation = xlCalculationManual


    and end it with

    Application.Calculation = acm

    ADDED: Also consider disabling event handlers at the beginning and reenabling them at the end.

  6. #6
    Registered User
    Join Date
    01-13-2020
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2019
    Posts
    8

    Re: Improve Speed: VBA Code very slow due to changing cell values in for loop

    Quote Originally Posted by jolivanes View Post
    Or do you mean it to be
    Please Login or Register  to view this content.
    Doh! Thank you - I'm kicking myself for not picking that up!

  7. #7
    Registered User
    Join Date
    01-13-2020
    Location
    Christchurch, New Zealand
    MS-Off Ver
    2019
    Posts
    8

    Re: Improve Speed: VBA Code very slow due to changing cell values in for loop

    Quote Originally Posted by jolivanes View Post
    Num_Unique_Codes should be
    Please Login or Register  to view this content.
    instead of the code you have now.
    The same for Numrows
    Please Login or Register  to view this content.

    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. Replace Code to improve processing speed for copy & paste
    By paula.mccall in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-29-2019, 01:04 PM
  2. For Loop Code is Very Slow - How can I speed it up?
    By maym in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2019, 03:53 AM
  3. How to improve performance of my code?, now is too slow!!
    By Laurelb in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-25-2015, 03:15 PM
  4. [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
  5. [SOLVED] How to improve speed of VBA code while using Vlookup function
    By Narasimharao Nandula in forum Excel Programming / VBA / Macros
    Replies: 38
    Last Post: 08-02-2013, 11:20 PM
  6. How to improve the running speed of this VBA macro code?
    By sellim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2012, 01:45 PM
  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