+ Reply to Thread
Results 1 to 9 of 9

How to optimise counting in loops

Hybrid View

  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!

    Sub Example4()
    Dim mcco As Worksheet, mcfc As Worksheet, mcfb As Worksheet, mcfv As Worksheet
    Dim CVTR As Long, FCBR As Long, FBCC As Long, FCMC As Long, ii As Long, i As Long, j As Long, k As Long, l As Long, s As Long
    Dim FTVA(), FTNA, MLNA
    Dim CVFD As Collection, CVMN As Collection, CVFP As Collection, CVFN As Collection, CVM1 As Collection, CVM2 As Collection
    Dim XD
    Dim pattern As String
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set mcco = Workbooks("PERSONAL.xlsb").Worksheets("Converter")
    Set mcfc = Workbooks("PERSONAL.xlsb").Worksheets("Files Count")
    Set mcfb = Workbooks("PERSONAL.xlsb").Worksheets("Files BSA Converter")
      
    CVTR = mcco.Cells(Rows.Count, 2).End(xlUp).Row 'Currently at 400439
    FCBR = mcfc.Cells(Rows.Count, 2).End(xlUp).Row - 1 'Currently at 870
    FBCC = Application.WorksheetFunction.Max(mcfc.Columns(6)) + 1 'Currently at 5
    FCMC = mcfc.Cells(1, Columns.Count).End(xlToLeft).Column 'Currently at 41
    
    Workbooks("PERSONAL.xlsb").Worksheets("Converter").Activate
    
    Set CVFD = New Collection
    Set CVMN = New Collection
    Set CVFP = New Collection
    Set CVFN = New Collection
    Set CVM1 = New Collection
    Set CVM2 = New Collection
    
    For i = 1 To CVTR
        CVFD.Add mcco.Cells(i, 1).Value
        CVMN.Add mcco.Cells(i, 2).Value
        CVFP.Add mcco.Cells(i, 3).Value
        CVFN.Add mcco.Cells(i, 4).Value
        CVM1.Add mcco.Cells(i, 2).Value & " || " & mcco.Cells(i, 3).Value
        CVM2.Add mcco.Cells(i, 2).Value & " || " & mcco.Cells(i, 4).Value
    Next i
    
    MLNA = Application.Transpose(Range(mcfc.Cells(1, 2), mcfc.Cells(FCBR, 2)))
    
    j = 1
    With mcfc
        FTNA = Application.Transpose(.Range(.Cells(1, 1), .Cells(1, FCMC)).Value)
        For i = 3 To FCMC
            ReDim Preserve FTVA(j)
            FTVA(j) = Mid(FTNA(i, 1), Application.Find("(", FTNA(i, 1)) + 1, Application.Find(")", FTNA(i, 1)) - Application.Find("(", FTNA(i, 1)) - 1)
            j = j + 1
        Next i
    End With
        
        For j = 1 To FCBR
            c = 0
            For k = 3 To 7
                For l = 2 To CVTR
                pattern = "*" & MLNA(j) & "*" & FTVA(k) & ""
                If CVM2.Item(l) Like pattern Then
                    mcfc.Cells(j + 1, k).Value = c + 1
                    c = mcfc.Cells(j + 1, k).Value
                End If
                Next l
                Stop
            Next k
        Next j
    
    Stop
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationManual
    
    End Sub
    Last edited by Geode7; 05-14-2019 at 10:08 AM.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    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 [★ Add Reputation] to left of post window...
    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

    Range("your_range").SpecialCells(xlCellTypeVisible).Copy
    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
    MS365 Apps for enterprise
    Posts
    5,973

    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.
        For i = 3 To FCMC
            ReDim Preserve FTVA(j)
            FTVA(j) = Mid(FTNA(i, 1), Application.Find("(", FTNA(i, 1)) + 1, Application.Find(")", FTNA(i, 1)) - Application.Find("(", FTNA(i, 1)) - 1)
            j = j + 1
        Next i
    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?

    Sub COUNTINGFILES()
    
        For i = 2 To FCBR
            For j = 3 To 7
                YD = "=COUNTIFS(" + CVR1.Address(External:=True) + ",""" & MLNA(i) & """," + CVR3.Address(External:=True) + ",""*""&""" & FTVA(j) & """, " + CVR2.Address(External:=True) + ", ""<>*\*"")"
                mcfc.Cells(i, j).Value = YD
                mcfc.Cells(i, j).Value = mcfc.Cells(i, j).Value
            Next j
        Next i
        
    
    FBCC = Application.WorksheetFunction.Max(mcfc.Columns(6)) + 1
    MBSA = WorksheetFunction.Application.Transpose(Range(mcfb.Cells(1, 1), mcfb.Cells(FCBR, FBCC)))
        
        For i = 2 To FCBR
            s = 0
            For j = 8 To 10
                For k = 2 To FBCC
                    ZD = "=COUNTIFS(" + CVR1.Address(External:=True) + ",""" & MBSA(k, i) & """," + CVR3.Address(External:=True) + ",""*""&""" & FTVA(j) & """)"
                    mcfc.Cells(i, j).Value = ZD
                    s = s + mcfc.Cells(i, j).Value
                    mcfc.Cells(i, j).Value = s
                Next k
            Next j
        Next i
    
    End Sub
    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