Need help: VBA is too slow to write 6 formulas

Cakz Primz

Board Regular
Joined
Dec 4, 2016
Messages
102
Office Version
  1. 365
Platform
  1. Windows
Dear all,

I have a code below to write 6 formulas and change the formula into values. Without any other workbook open, it takes more than 600 seconds to execute the code.
Is there any solution to speed it up?


VBA Code:
Sub ExtractPONumber_MSRLineItem()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

    Dim t As Double: t = Timer
    
    Workbooks.Open fileName:="P:\12.COMMON_FOLDER\PRIMA\MSR.xlsb"
    Windows("ZJMU_2020.xlsb").Activate
    Sheet3.Activate
    
    Dim ws As Worksheet
    Dim lRow As Long
    lRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    Set ws = ActiveSheet
    
    With ws
    .Range("C2:C" & lRow).FormulaR1C1 = "=VALUE(TEXTAFTER(RC[-1],""_""))"
    .Range("D2:D" & lRow).FormulaR1C1 = "=SUM(--(LEN(UNIQUE(FILTER([MSR.xlsb]DataSource!R4C12:R150000C12,[MSR.xlsb]DataSource!R4C42:R150000C42=RC[-2],"""")))>0))"
    .Range("E2:E" & lRow).FormulaR1C1 = "=SUM(--(LEN(UNIQUE(FILTER(MRR!R3C11:R600000C11,MRR!R3C2:R600000C2=RC[-2],"""")))>0))"
    .Range("F2:F" & lRow).FormulaR1C1 = "=SUM(--(LEN(UNIQUE(FILTER(MRR!R3C11:R600000C11,MRR!R3C2:R600000C2=RC[-3],"""")))>0))"
    .Range("G2:G" & lRow).FormulaR1C1 = "=XLOOKUP(RC[-4],ZJMU!C[9],ZJMU!C[11])"
    .Range("H2:H" & lRow).FormulaR1C1 = "=IF(AND(RC[-4]>0,RC[-2]=0),0,1)"
    .Range("C2:H" & lRow).Value = .Range("C2:H" & lRow).Value
    End With

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

    Application.DisplayAlerts = False
    Workbooks("MSR.xlsb").Close SaveChanges:=False
    Application.DisplayAlerts = True
    
    MsgBox "Finish in " & Timer - t & " seconds", , "Luce"

End Sub

Thank you very much in advanced.
Best regards,
Prima
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
There is no point in turning calculation off and then back on with that code.
 
Upvote 0
Because if you use .Range("C2:C" & lRow).FormulaR1C1 the formulas have to be calculated to populate the cells with something (otherwise this code wouldn’t work at all since they are replaced with values before turning calculation back on). It is most likely slowing the code down changing the calculation mode.
 
Upvote 0
Because if you use .Range("C2:C" & lRow).FormulaR1C1 the formulas have to be calculated to populate the cells with something (otherwise this code wouldn’t work at all since they are replaced with values before turning calculation back on). It is most likely slowing the code down changing the calculation mode.
Ok. Thank you for the information. I wonder how OP has been using it then.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top