VBA copy rows fast



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?



Sub copy_rows()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False

Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrow = Lastrow * 4

For i = 1 To Lastrow Step 4
Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
Rows(i).Copy Destination:=Rows(i + 1)
Rows(i).Copy Destination:=Rows(i + 2)
Rows(i).Copy Destination:=Rows(i + 3)
Next i

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

End Sub


Thank you very much










share|improve this question




























    0















    I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
    My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?



    Sub copy_rows()

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False

    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Lastrow = Lastrow * 4

    For i = 1 To Lastrow Step 4
    Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
    Rows(i).Copy Destination:=Rows(i + 1)
    Rows(i).Copy Destination:=Rows(i + 2)
    Rows(i).Copy Destination:=Rows(i + 3)
    Next i

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True

    End Sub


    Thank you very much










    share|improve this question
























      0












      0








      0








      I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
      My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?



      Sub copy_rows()

      Application.Calculation = xlCalculationManual
      Application.ScreenUpdating = False
      Application.DisplayStatusBar = False

      Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
      Lastrow = Lastrow * 4

      For i = 1 To Lastrow Step 4
      Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
      Rows(i).Copy Destination:=Rows(i + 1)
      Rows(i).Copy Destination:=Rows(i + 2)
      Rows(i).Copy Destination:=Rows(i + 3)
      Next i

      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
      Application.DisplayStatusBar = True

      End Sub


      Thank you very much










      share|improve this question














      I have to work on files with 5000 rows, for each row I have to insert 3 more rows and copy the content in these new rows (after that there will be more steps).
      My macro works fine but the process of copying the content is really slow, I´m sure there is a solution that works better, any ideas?



      Sub copy_rows()

      Application.Calculation = xlCalculationManual
      Application.ScreenUpdating = False
      Application.DisplayStatusBar = False

      Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
      Lastrow = Lastrow * 4

      For i = 1 To Lastrow Step 4
      Cells(i, 7).EntireRow.Offset(1).Resize(3).Insert Shift:=xlDown
      Rows(i).Copy Destination:=Rows(i + 1)
      Rows(i).Copy Destination:=Rows(i + 2)
      Rows(i).Copy Destination:=Rows(i + 3)
      Next i

      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
      Application.DisplayStatusBar = True

      End Sub


      Thank you very much







      excel vba performance copy rows






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 9:18









      Marco CMarco C

      32




      32






















          3 Answers
          3






          active

          oldest

          votes


















          2














          When it comes to speed:

          Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.



          So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.



          The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.



          Drawback: you maybe have to take care about formatting



          With ActiveSheet
          Dim lastRow As Long, lastCol As Long

          lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
          lastRow = .Cells(.Rows.Count, 1).End(xlUp).row

          Dim origData, copyData
          origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
          ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
          Dim r As Long, c As Long, i As Long
          For r = 1 To lastRow ' All rows in orig data
          For c = 1 To lastCol ' All columns in orig data
          For i = 1 To 4 ' Copy everything 4 times
          copyData((r - 1) * 4 + i, c) = origData(r, c)
          Next i
          Next c
          Next r
          .Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet

          End With





          share|improve this answer























          • This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

            – Marco C
            Nov 19 '18 at 8:17


















          0














          Probably the fastest way, if you are not interested in format, but only in the values:



          Sub TestMe()

          With Worksheets(1)
          .Rows(1).Value = .Rows(2).Value
          End With

          End Sub





          share|improve this answer






























            0














            FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.



            Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.



            Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.



            Sub copy_rows2()

            Application.Calculation = xlCalculationManual
            Application.ScreenUpdating = False
            Application.DisplayStatusBar = False

            dim c as integer
            c = 10 'number of columns with data

            lastRow = Cells(Rows.Count, "A").End(xlUp).Row
            lastRow = lastRow * 4

            For i = 1 To lastRow Step 4

            'inserts 3 rows at a time
            ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown

            'copy data into new rows limited to number of columns c
            Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value

            Next i

            Application.Calculation = xlCalculationAutomatic
            Application.ScreenUpdating = True
            Application.DisplayStatusBar = True

            End Sub





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316021%2fvba-copy-rows-fast%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              When it comes to speed:

              Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.



              So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.



              The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.



              Drawback: you maybe have to take care about formatting



              With ActiveSheet
              Dim lastRow As Long, lastCol As Long

              lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
              lastRow = .Cells(.Rows.Count, 1).End(xlUp).row

              Dim origData, copyData
              origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
              ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
              Dim r As Long, c As Long, i As Long
              For r = 1 To lastRow ' All rows in orig data
              For c = 1 To lastCol ' All columns in orig data
              For i = 1 To 4 ' Copy everything 4 times
              copyData((r - 1) * 4 + i, c) = origData(r, c)
              Next i
              Next c
              Next r
              .Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet

              End With





              share|improve this answer























              • This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

                – Marco C
                Nov 19 '18 at 8:17















              2














              When it comes to speed:

              Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.



              So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.



              The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.



              Drawback: you maybe have to take care about formatting



              With ActiveSheet
              Dim lastRow As Long, lastCol As Long

              lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
              lastRow = .Cells(.Rows.Count, 1).End(xlUp).row

              Dim origData, copyData
              origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
              ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
              Dim r As Long, c As Long, i As Long
              For r = 1 To lastRow ' All rows in orig data
              For c = 1 To lastCol ' All columns in orig data
              For i = 1 To 4 ' Copy everything 4 times
              copyData((r - 1) * 4 + i, c) = origData(r, c)
              Next i
              Next c
              Next r
              .Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet

              End With





              share|improve this answer























              • This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

                – Marco C
                Nov 19 '18 at 8:17













              2












              2








              2







              When it comes to speed:

              Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.



              So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.



              The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.



              Drawback: you maybe have to take care about formatting



              With ActiveSheet
              Dim lastRow As Long, lastCol As Long

              lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
              lastRow = .Cells(.Rows.Count, 1).End(xlUp).row

              Dim origData, copyData
              origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
              ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
              Dim r As Long, c As Long, i As Long
              For r = 1 To lastRow ' All rows in orig data
              For c = 1 To lastCol ' All columns in orig data
              For i = 1 To 4 ' Copy everything 4 times
              copyData((r - 1) * 4 + i, c) = origData(r, c)
              Next i
              Next c
              Next r
              .Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet

              End With





              share|improve this answer













              When it comes to speed:

              Accessing Excel data in VBA is slow, inserting a row (or column) is insane slow, while everything done in memory (VBA variables) is so fast that you can nearly not measure it.



              So my suggestion is to read all the data from your worksheet into memory, "multiply" the rows there and write everything back all at once.



              The following code example reads the data in a 2-dimensional array and copy it into a 2nd array that's 4 times as large. This 2nd array is written back to the sheet. I tested it with 1000 rows and execution time was 0s.



              Drawback: you maybe have to take care about formatting



              With ActiveSheet
              Dim lastRow As Long, lastCol As Long

              lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
              lastRow = .Cells(.Rows.Count, 1).End(xlUp).row

              Dim origData, copyData
              origData = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)) ' Read data from sheet
              ReDim copyData(1 To lastRow * 4, 1 To lastCol) ' new array is 4 times the size
              Dim r As Long, c As Long, i As Long
              For r = 1 To lastRow ' All rows in orig data
              For c = 1 To lastCol ' All columns in orig data
              For i = 1 To 4 ' Copy everything 4 times
              copyData((r - 1) * 4 + i, c) = origData(r, c)
              Next i
              Next c
              Next r
              .Range(.Cells(1, 1), .Cells(lastRow * 4, lastCol)) = copyData ' Write back to sheet

              End With






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 10:13









              FunThomasFunThomas

              5,5161626




              5,5161626












              • This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

                – Marco C
                Nov 19 '18 at 8:17

















              • This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

                – Marco C
                Nov 19 '18 at 8:17
















              This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

              – Marco C
              Nov 19 '18 at 8:17





              This is brilliant, I knew I had to work with th ememory but didn´t know where to begin Thank you so much

              – Marco C
              Nov 19 '18 at 8:17













              0














              Probably the fastest way, if you are not interested in format, but only in the values:



              Sub TestMe()

              With Worksheets(1)
              .Rows(1).Value = .Rows(2).Value
              End With

              End Sub





              share|improve this answer



























                0














                Probably the fastest way, if you are not interested in format, but only in the values:



                Sub TestMe()

                With Worksheets(1)
                .Rows(1).Value = .Rows(2).Value
                End With

                End Sub





                share|improve this answer

























                  0












                  0








                  0







                  Probably the fastest way, if you are not interested in format, but only in the values:



                  Sub TestMe()

                  With Worksheets(1)
                  .Rows(1).Value = .Rows(2).Value
                  End With

                  End Sub





                  share|improve this answer













                  Probably the fastest way, if you are not interested in format, but only in the values:



                  Sub TestMe()

                  With Worksheets(1)
                  .Rows(1).Value = .Rows(2).Value
                  End With

                  End Sub






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 9:23









                  VityataVityata

                  32.5k72453




                  32.5k72453





















                      0














                      FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.



                      Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.



                      Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.



                      Sub copy_rows2()

                      Application.Calculation = xlCalculationManual
                      Application.ScreenUpdating = False
                      Application.DisplayStatusBar = False

                      dim c as integer
                      c = 10 'number of columns with data

                      lastRow = Cells(Rows.Count, "A").End(xlUp).Row
                      lastRow = lastRow * 4

                      For i = 1 To lastRow Step 4

                      'inserts 3 rows at a time
                      ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown

                      'copy data into new rows limited to number of columns c
                      Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value

                      Next i

                      Application.Calculation = xlCalculationAutomatic
                      Application.ScreenUpdating = True
                      Application.DisplayStatusBar = True

                      End Sub





                      share|improve this answer



























                        0














                        FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.



                        Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.



                        Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.



                        Sub copy_rows2()

                        Application.Calculation = xlCalculationManual
                        Application.ScreenUpdating = False
                        Application.DisplayStatusBar = False

                        dim c as integer
                        c = 10 'number of columns with data

                        lastRow = Cells(Rows.Count, "A").End(xlUp).Row
                        lastRow = lastRow * 4

                        For i = 1 To lastRow Step 4

                        'inserts 3 rows at a time
                        ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown

                        'copy data into new rows limited to number of columns c
                        Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value

                        Next i

                        Application.Calculation = xlCalculationAutomatic
                        Application.ScreenUpdating = True
                        Application.DisplayStatusBar = True

                        End Sub





                        share|improve this answer

























                          0












                          0








                          0







                          FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.



                          Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.



                          Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.



                          Sub copy_rows2()

                          Application.Calculation = xlCalculationManual
                          Application.ScreenUpdating = False
                          Application.DisplayStatusBar = False

                          dim c as integer
                          c = 10 'number of columns with data

                          lastRow = Cells(Rows.Count, "A").End(xlUp).Row
                          lastRow = lastRow * 4

                          For i = 1 To lastRow Step 4

                          'inserts 3 rows at a time
                          ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown

                          'copy data into new rows limited to number of columns c
                          Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value

                          Next i

                          Application.Calculation = xlCalculationAutomatic
                          Application.ScreenUpdating = True
                          Application.DisplayStatusBar = True

                          End Sub





                          share|improve this answer













                          FunThomas is right and that should be the quickest way, but if that's not an option it's a lot quicker not to copy the whole row.



                          Defining a range and just copying the data in those cells is a lot data than the thousands of columns in the sheet and I doubt your spreadsheet uses all of them.



                          Also as Vitaya said it's quicker to just copy the values and you can always bulk format the whole lot afterwards if it's required.



                          Sub copy_rows2()

                          Application.Calculation = xlCalculationManual
                          Application.ScreenUpdating = False
                          Application.DisplayStatusBar = False

                          dim c as integer
                          c = 10 'number of columns with data

                          lastRow = Cells(Rows.Count, "A").End(xlUp).Row
                          lastRow = lastRow * 4

                          For i = 1 To lastRow Step 4

                          'inserts 3 rows at a time
                          ActiveSheet.Rows(i + 1 & ":" & i + 3).Insert Shift:=xlDown

                          'copy data into new rows limited to number of columns c
                          Range(Cells(i + 1, 1), Cells(i + 3, c)).Value = Range(Cells(i, 1), Cells(i, c)).Value

                          Next i

                          Application.Calculation = xlCalculationAutomatic
                          Application.ScreenUpdating = True
                          Application.DisplayStatusBar = True

                          End Sub






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 11:45









                          KagekibaKagekiba

                          357




                          357



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316021%2fvba-copy-rows-fast%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20