Results 1 to 9 of 9

How to optimise counting in loops

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

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