+ Reply to Thread
Results 1 to 7 of 7

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

Hybrid View

  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:

    Sub Filtering()
    Application.ScreenUpdating = False
    Numrows = Range("L6").Value
    Num_Unique_Codes = Range("J6").Value
    
    For i = 20 To Num_Unique_Codes
        Name = Range("O20:O" & i)
        Range("A19:L" & Numrows).AutoFilter Field:=7, Criteria1:=Name
        Range("K5") = Application.Sum(Range("C20:C" & Numrows).SpecialCells(xlCellTypeVisible))
        If Range("K5") = 0 Then
            Range("A20:L" & Numrows).SpecialCells(xlCellTypeVisible).Clear
    
        End If
        
    ActiveSheet.ShowAllData
    Next i
    
    
    Application.ScreenUpdating = True
    End Sub
    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,705

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

    You have
    For i = 20 To NUC (=23)
    So for the first Loop that results in
    Name = Range("O20:O20") '(= Person1)
        'Do something here
    On the next Loop, the result is
    Name = Range("O20:O21") '(= Person1 and Person2)
    On the Loop after that
    Name = Range("O20:O22") '(Person1, Person2 and Person3)
    On the last Loop you'll get
    Name = Range("O20:O23")  '(Person1, Person2, Person3 and Person5)
    Is that what you want?
    Or do you mean it to be
    Name = Cells(i, 15).Value

  3. #3
    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
    Name = Cells(i, 15).Value
    Doh! Thank you - I'm kicking myself for not picking that up!

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

    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.

    Sub Maybe()
    Dim k As Long, a() As String, i As Long, x As String, j As Long, c As Range, y As Double
    Application.ScreenUpdating = False
    k = 1
    ReDim a(1 To k)
    a(1) = Cells(20, 7).Value
    i = 21
        While Not IsEmpty(Cells(i, 7))
            x = Cells(i, 7).Value
            If IsError(Application.Match(x, a, 0)) Then
                k = k + 1
                ReDim Preserve a(1 To k)
                a(k) = x
            End If
            i = i + 1
        Wend
        For j = LBound(a) To UBound(a)
            y = 0
        For Each c In Range("G20:G" & Cells(Rows.Count, "G").End(xlUp).Row)
            If c.Value = a(j) Then y = y + c.Offset(, -4).Value
        Next c
        If y = 0 Then
        For Each c In Range("G20:G" & Cells(Rows.Count, "G").End(xlUp).Row)
            If c.Value = a(j) Then c.Offset(, -6).Resize(, 7).ClearContents
        Next c
        Else
            Cells(Rows.Count, "P").End(xlUp).Offset(1).Value = y
        End If
        Next j
    Application.ScreenUpdating = True
    End Sub

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

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

    Num_Unique_Codes should be
    Cells(Rows.Count, "O").End(xlUp).Row
    instead of the code you have now.
    The same for Numrows
    Cells(Rows.Count, 1).End(xlUp).Row

  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
    Num_Unique_Codes should be
    Cells(Rows.Count, "O").End(xlUp).Row
    instead of the code you have now.
    The same for Numrows
    Cells(Rows.Count, 1).End(xlUp).Row

    Thank you.

  7. #7
    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.

+ 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