Speed Up VBA Code

jocker_boy

Board Regular
Joined
Feb 5, 2015
Messages
83
Hi,

My excel file have 3000 lines and some times cane have 6000 lines or 9000 lines.
I have the code below to fill 3 columns with formula - SUB1()
And to insert subtotal formulas in those 3 columns based on the lenght (columns B).

It's working 100%, but it takes a near 2 minutes each time i run, and i would like to reduce this time.
Is there any way to write this code (SUB2()) to speed up?

I tried to force Calculation Mode to Manual, but the code SUB2 don't run with CalculationManual.

Thanks very much for any help.
Gonçalo

VBA Code:
Sub SUB1()
    
    Dim lr As Long
    
'   Find last row in column E with data
    lr = Cells(Rows.Count, "F").End(xlUp).Row
    
'   Copy Range to last column down for all rows
    Range(Cells(6, "A"), Cells(6, "c")).Copy Range(Cells(7, "A"), Cells(lr, "C"))
    
'Apply total formulas
    
    Range(Cells(6, "S"), Cells(lr, "S")).Formula = "=$I6*Q6"
    Range(Cells(6, "U"), Cells(lr, "U")).Formula = "=$I6*J6"
    Range(Cells(6, "AO"), Cells(lr, "AO")).Formula = "=ROUND($I6*AK6,2)"
       
End Sub

Sub SUB2()

Dim r As Long, r2 As Long, last_row As Long
Dim next_row As Long, current_len As Long, test_len As Long
Dim rng As String

Dim i As Integer
Dim cols()
cols = [{"S","U","AO"}]

With ActiveSheet

last_row = .Cells(Rows.Count, 1).End(xlUp).Row

For i = LBound(cols) To UBound(cols)
For r = 6 To last_row
    next_row = r + 1

    If .Range("B" & next_row) > .Range("B" & r) Then
       current_len = .Range("B" & r)
      
       'create range
       For r2 = r + 1 To last_row
            test_len = .Range("B" & r2)
            If current_len >= test_len Then
                rng = cols(i) & r + 1 & ":" & cols(i) & r2 - 1
                Exit For
            End If
        Next
    
        .Range(cols(i) & r).Formula = "=SUBTOTAL(9," & rng & ")"
    End If
    
Next
Next

End With
End Sub

Sub SUBTOTAL()

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayStatusBar = False
    '.Calculation = xlCalculationManual
End With
 
    Call SUB1
    Call SUB2
 
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayStatusBar = True
    '.Calculation = xlCalculationAutomatic
End With

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I did it :)
i force the manual calculation only in SUB2()

VBA Code:
Sub SUBTOTAL()

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayStatusBar = False
End With
 
    Call SUB1
Application.Calculation = xlCalculationManual
    Call SUB2
Application.Calculation = xlCalculationAutomatic
 
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayStatusBar = True
End With

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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