Results 1 to 13 of 13

Thread: Advice needed to speed up Painting code based on defined colors.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Unfortunately, the computational complexity of the inserted formula is quite high, which will result in a long execution time.
    Comments.
    1) I don't know if you are in good control of the calculation process in the workbook. What is the purpose of enabling automatic calculation in this snippet?
        With Range("CK12:IC" & lLR)
            .Formula = "=IF(ISERROR(IF(CK$10<$BK12,0,IF(CK$10>=$BK12,IF(AND(CK$10=$BK12,$BE12<>"""")(...)"
            .Application.Calculation = xlCalculationAutomatic
            .Value = .Value
        End With
    Inserting into a range of formulas already calculates them. If the inserted formulas affect other cells (beyond the range inserted), it makes sense to enable automatic calculation. But in this case, I found no connections.
    If you want to calculate a workbook without turning on automatic calculation, you can do it using one of the methods:
    Application.CalculateFull
    Application.Calculate
    Worksheet.Calculate
    Range.Calculate
    Either you have to accept this macro execution time, or you could shorten the formula's calculation chain. You may also consider changing the formulas to a VBA procedure. While formulas are quite fast, there are times when VBA is faster.
    2) Conditional formatting also has a big impact on the computation speed (the less CFs the better). Review the CF list and delete unnecessary.
    3) The only thing I managed to shorten was the Launching_Code operation time by about 30%. But it is too little consolation for you.
    Sub Launching_Code()
    
        Dim j           As Long
    
        Dim LAUNCHINGCOLOR As Long
        Dim OrangeRefColor As Long
        Dim RedRefColor As Long
    
        Dim cntGreen    As Long
        Dim cntOrange   As Long
        Dim cntRed      As Long
    
        Dim vCounters   As Variant
        Dim rngCell     As Range
    
        Dim start_Time#, End_Time#
            
        start_Time = Timer
    
        Sheets("Main").Activate
        ActiveSheet.Unprotect
    
        LAUNCHINGCOLOR = Range("LAUNCHING").DisplayFormat.Interior.Color
        '    OrangeRefColor = Range("OrangeRef").DisplayFormat.Interior.Color
        '    RedRefColor = Range("RedRef").DisplayFormat.Interior.Color
    
        ReDim vCounters(1 To (Range("Loading_Zone1").Columns.Count))
    
        Application.ScreenUpdating = False
    
        For j = 1 To Range("Loading_Zone1").Columns.Count
            cntGreen = 0
            cntOrange = 0
            cntRed = 0
    
            For Each rngCell In Range("Loading_Zone1").Columns(j).Cells
    
                Select Case rngCell.DisplayFormat.Interior.Color
                    Case LAUNCHINGCOLOR
    
                        cntGreen = cntGreen + 1
    
                    Case OrangeRefColor
    
                        cntOrange = cntOrange + 1
    
                    Case RedRefColor
    
                        cntRed = cntRed + 1
    
                End Select
    
            Next rngCell
    
            'Cells(Range("LAUNCHING").Row, j) = cntGreen
            'Cells(Range("OrangeRef").Row, j) = cntOrange
            'Cells(Range("RedRef").Row, j) = cntRed
    
            vCounters(j) = cntGreen
        Next j
    
        Range("LAUNCHING").Offset(, 2).Resize(, UBound(vCounters)).Value = vCounters
    
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, _
                            AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                            AllowFormattingRows:=True, AllowDeletingRows:=True, AllowSorting:=True, _
                            AllowFiltering:=True
    
        Application.ScreenUpdating = True
    
        End_Time = Timer
        
        Debug.Print "Launching_Code: " & Format(End_Time - start_Time, "0.000")
    
    End Sub
    Artik
    Last edited by Artik; 08-09-2020 at 07:06 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •