Need help with macro

All2Cheesy

Board Regular
Joined
Mar 4, 2015
Messages
127
I've created a Macro that will copy what's in cells and then insert that X amount of times in the cells below(Being used to copy formulas). However, the macro takes far too long to run, rendering it impractical. Could someone please assist in optimising my code so that it can run a bit quicker? Thanks in advance!


Code:
Sub AddLines()

'Enable additional feautres
With Application
        .ScreenUpdating = False
        .Calculation = Manual
        .EnableEvents = False
        .DisplayAlerts = False
End With

Dim RepeatFactor As Variant
        RepeatFactor = Cells.Range("Q7")
        
         ILoop = 1
         
         Do While ILoop <= RepeatFactor
            ActiveSheet.Range("A5023:AG5023").Copy
            ActiveSheet.Range("A5023:AG5023").Select
            Selection.Insert Shift:=xlDown
           
            ILoop = ILoop + 1
         Loop
         
    Range("A5024:G65000").Select
    Selection.ClearContents
    Range("A5024:L65000").Select
    Selection.ClearContents
    Range("A5024:Q65000").Select
    Selection.ClearContents
    
'Enable additional feautres
With Application
        .ScreenUpdating = True
        .Calculation = xlAutomatic
        .EnableEvents = True
        .DisplayAlerts = True
End With

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try, but I'm not sure you screenupdating, enableveents OR DisplayAlerts !!!
Code:
Sub AddLines()
Application.Calculation = Manual
Range("A5023:AG5023").Copy
Range("A5023:AG5023").Resize(Range("Q7").Value).Insert Shift:=xlDown
Application.Calculation = xlAutomatic
End Sub
 
Last edited:
Upvote 0
Try, but I'm not sure you screenupdating, enableveents OR DisplayAlerts !!!
Code:
Sub AddLines()
Application.Calculation = Manual
Range("A5023:AG5023").Copy
Range("A5023:AG5023").Resize(Range("Q7").Value).Insert Shift:=xlDown
Application.Calculation = xlAutomatic
End Sub

That's worked perfectly. Thanks for that!
 
Upvote 0

Forum statistics

Threads
1,223,606
Messages
6,173,323
Members
452,510
Latest member
RCan29

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