Home » excel » excel – Need to optimize a slow vba code

excel – Need to optimize a slow vba code

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using following code and it is taking forever to complete-

For i = 1 To 21408
    Range("abcd").Copy
    Rows(Range("abcd").Offset(i, 0).Row).Insert Shift:=xlDown
    Application.CutCopyMode = False
Next i

abcd is Named Range having 8 fields.

Can someone please suggest a way to optimize this code or re-write in a way to achieve the same output?

Thanks

How to&Answers:

This does the same thing, much faster:

With Range("abcd")
    .Copy
    .Offset(1).Resize(21408, .Columns.Count).Insert Shift:=xlDown
    Application.CutCopyMode = False
End With