Speed up

PIsabel

Board Regular
Joined
Feb 4, 2014
Messages
126
Office Version
  1. 365
Platform
  1. Windows
Hello.
How can I speed up this code?
My file is from line 2 to line 30000
my macro inserts the formulas in line 2 and then copies and pastes from line 3 to 30000
"Range("B2:C2").Select" and Range("F2:AI2").Select have long formulas which makes the file very heavy. The macro itself is struggling
Most of the time I only have half a dozen lines to use.
What I need is a code that only runs up to the last cell in column E

Thanks



VBA Code:
    'copy and paste formulas
    Range("B2:C2").Select
    Selection.Copy
    Range("B3:C30000").Select
    ActiveSheet.Paste

    'copy and paste formulas
    Range("F2:AI2").Select
    Selection.Copy
    Range("F3:AI30000").Select
    ActiveSheet.Paste
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Am no VBA expert but I often see people say remove the Select for a start
If you're just copying formulas, maybe simply

Code:
Range("B3:C30000") = Range("B2:C2").Formula
Range("F3:AI30000") = Range("F2:AI2").Formula
 
Upvote 0
Try:

VBA Code:
Sub CopyFormulaDown()

    Dim ws As Worksheet
    Dim LastRow As Long
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set ws = ActiveSheet
    
    With ws
        LastRow = .Range("E" & Rows.Count).End(xlUp).Row
        .Range("B2:C" & LastRow).Formula = .Range("B2:C2").Formula
        .Range("F2:AI" & LastRow).Formula = .Range("F2:AI2").Formula
    End With
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Upvote 1
Solution
Thanks Alex Blakenburg

The code works perfectly.
And if I want to copy data from one sheet to another, is there a way to change this code to work from the "AAA" sheet to the "BBB" sheet?
 
Upvote 0
Using the assignment approach (.Formula = .Formula) on multiple columns caused some relative reference issues when I used it to copy to mulitple rows at a different location, so I have gone back to using copy-paste. I think most of the speed gains were in "turning off" screenupdating and calculations but let me know if this is too slow.

VBA Code:
Sub CopyFormulaDown_DifferentSheet_Copy()

    Dim wsSrc As Worksheet, wsDest As Worksheet
    Dim LastRow As Long
 
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
 
    Set wsSrc = Worksheets("Sheet1")                ' <--- Change Sheet1 to sheet containing formula
    Set wsDest = Worksheets("Sheet2")               ' <--- Change Sheet2 to sheet to copy to
 
    With wsDest
        LastRow = .Range("E" & Rows.Count).End(xlUp).Row
        wsSrc.Range("B2:C2").Copy Destination:=.Range("B2:C" & LastRow)
        wsSrc.Range("F2:AI2").Copy Destination:=.Range("F2:AI" & LastRow)
    End With
 
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

If it is too slow you could try replacing the With wsDest section with this:
VBA Code:
    With wsDest
        LastRow = .Range("E" & Rows.Count).End(xlUp).Row
        .Range("B2:C2").Formula = wsSrc.Range("B2:C2").Formula
        .Range("B2:C" & LastRow).FillDown
        .Range("F2:AI2").Formula = wsSrc.Range("F2:AI2").Formula
        .Range("F2:AI" & LastRow).FillDown
    End With
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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