How can I copy specific rows from several worksheets to another with Excel VBA?












0















Every month end I have to copy data (say rows 4 to the last row of some worksheets (say worksheets 2 - 7, but in fact there are numerous worksheets and rows) to worksheet 1, start the pasting from row 4 of worksheet 1.



I do try my very best to look out the answer from stack overflow and other website. Given I am a layman of VBA Excel, I can't really customize those code to my case.



I would be very grateful if anyone could give me a hand. Thank you.



Below is a fail example I do by myself



Sub test1()
Workbooks.Open Filename:="file location"
Worksheets(2).Activate
a = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

For a1 = 3 To a
Worksheets(2).Row(a1).Copy
Worksheets(1).Activate
Worksheets(1).Rows(a).PasteSpecial
Next
End Sub


Thank you alex. i amended the code by reference to yours.
I subsequently found writing my code like below does not really efficient while there a around 100 worksheets to be copied and 7 master lists to be pasted. Below is the revised code, it seems more efficient to be written by "do if" function? Say once the VBA copied worksheets 2-10 rows 4 to worksheet 1, then start copying worksheets 12-21 rows 4 to worksheet 21



Sub test2()

'
Workbooks.Open Filename:="file location"

Worksheets(2).Rows("1:3").Copy Destination:=Worksheets(1).Rows(1)
Worksheets(1).Range("A:AG").ColumnWidth = 20
Worksheets(1).Range("AD:AD").ColumnWidth = 65
'
'
Worksheets(2).Activate
a = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

Set rangeToCopy = Worksheets(2).Range(Cells(4, 1), Cells(a, 33))
rangeToCopy.Copy
Dim lastRow As String
lastRow = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
Worksheets(1).Cells(lastRow, 1).PasteSpecial ' lastRow,1

Application.CutCopyMode = False
Set rangeToCopy = Nothing
'
'
'
Worksheets(3).Activate
b = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

Set rangeToCopy = Worksheets(3).Range(Cells(4, 1), Cells(b, 33))
rangeToCopy.Copy
Dim lastRow2 As String
lastRow2 = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
Worksheets(1).Cells(lastRow2, 1).PasteSpecial ' lastRow2,1

Application.CutCopyMode = False
Set rangeToCopy = Nothing

.....to be continued
End Sub









share|improve this question





























    0















    Every month end I have to copy data (say rows 4 to the last row of some worksheets (say worksheets 2 - 7, but in fact there are numerous worksheets and rows) to worksheet 1, start the pasting from row 4 of worksheet 1.



    I do try my very best to look out the answer from stack overflow and other website. Given I am a layman of VBA Excel, I can't really customize those code to my case.



    I would be very grateful if anyone could give me a hand. Thank you.



    Below is a fail example I do by myself



    Sub test1()
    Workbooks.Open Filename:="file location"
    Worksheets(2).Activate
    a = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For a1 = 3 To a
    Worksheets(2).Row(a1).Copy
    Worksheets(1).Activate
    Worksheets(1).Rows(a).PasteSpecial
    Next
    End Sub


    Thank you alex. i amended the code by reference to yours.
    I subsequently found writing my code like below does not really efficient while there a around 100 worksheets to be copied and 7 master lists to be pasted. Below is the revised code, it seems more efficient to be written by "do if" function? Say once the VBA copied worksheets 2-10 rows 4 to worksheet 1, then start copying worksheets 12-21 rows 4 to worksheet 21



    Sub test2()

    '
    Workbooks.Open Filename:="file location"

    Worksheets(2).Rows("1:3").Copy Destination:=Worksheets(1).Rows(1)
    Worksheets(1).Range("A:AG").ColumnWidth = 20
    Worksheets(1).Range("AD:AD").ColumnWidth = 65
    '
    '
    Worksheets(2).Activate
    a = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

    Set rangeToCopy = Worksheets(2).Range(Cells(4, 1), Cells(a, 33))
    rangeToCopy.Copy
    Dim lastRow As String
    lastRow = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
    Worksheets(1).Cells(lastRow, 1).PasteSpecial ' lastRow,1

    Application.CutCopyMode = False
    Set rangeToCopy = Nothing
    '
    '
    '
    Worksheets(3).Activate
    b = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

    Set rangeToCopy = Worksheets(3).Range(Cells(4, 1), Cells(b, 33))
    rangeToCopy.Copy
    Dim lastRow2 As String
    lastRow2 = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
    Worksheets(1).Cells(lastRow2, 1).PasteSpecial ' lastRow2,1

    Application.CutCopyMode = False
    Set rangeToCopy = Nothing

    .....to be continued
    End Sub









    share|improve this question



























      0












      0








      0








      Every month end I have to copy data (say rows 4 to the last row of some worksheets (say worksheets 2 - 7, but in fact there are numerous worksheets and rows) to worksheet 1, start the pasting from row 4 of worksheet 1.



      I do try my very best to look out the answer from stack overflow and other website. Given I am a layman of VBA Excel, I can't really customize those code to my case.



      I would be very grateful if anyone could give me a hand. Thank you.



      Below is a fail example I do by myself



      Sub test1()
      Workbooks.Open Filename:="file location"
      Worksheets(2).Activate
      a = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

      For a1 = 3 To a
      Worksheets(2).Row(a1).Copy
      Worksheets(1).Activate
      Worksheets(1).Rows(a).PasteSpecial
      Next
      End Sub


      Thank you alex. i amended the code by reference to yours.
      I subsequently found writing my code like below does not really efficient while there a around 100 worksheets to be copied and 7 master lists to be pasted. Below is the revised code, it seems more efficient to be written by "do if" function? Say once the VBA copied worksheets 2-10 rows 4 to worksheet 1, then start copying worksheets 12-21 rows 4 to worksheet 21



      Sub test2()

      '
      Workbooks.Open Filename:="file location"

      Worksheets(2).Rows("1:3").Copy Destination:=Worksheets(1).Rows(1)
      Worksheets(1).Range("A:AG").ColumnWidth = 20
      Worksheets(1).Range("AD:AD").ColumnWidth = 65
      '
      '
      Worksheets(2).Activate
      a = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

      Set rangeToCopy = Worksheets(2).Range(Cells(4, 1), Cells(a, 33))
      rangeToCopy.Copy
      Dim lastRow As String
      lastRow = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
      Worksheets(1).Cells(lastRow, 1).PasteSpecial ' lastRow,1

      Application.CutCopyMode = False
      Set rangeToCopy = Nothing
      '
      '
      '
      Worksheets(3).Activate
      b = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

      Set rangeToCopy = Worksheets(3).Range(Cells(4, 1), Cells(b, 33))
      rangeToCopy.Copy
      Dim lastRow2 As String
      lastRow2 = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
      Worksheets(1).Cells(lastRow2, 1).PasteSpecial ' lastRow2,1

      Application.CutCopyMode = False
      Set rangeToCopy = Nothing

      .....to be continued
      End Sub









      share|improve this question
















      Every month end I have to copy data (say rows 4 to the last row of some worksheets (say worksheets 2 - 7, but in fact there are numerous worksheets and rows) to worksheet 1, start the pasting from row 4 of worksheet 1.



      I do try my very best to look out the answer from stack overflow and other website. Given I am a layman of VBA Excel, I can't really customize those code to my case.



      I would be very grateful if anyone could give me a hand. Thank you.



      Below is a fail example I do by myself



      Sub test1()
      Workbooks.Open Filename:="file location"
      Worksheets(2).Activate
      a = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

      For a1 = 3 To a
      Worksheets(2).Row(a1).Copy
      Worksheets(1).Activate
      Worksheets(1).Rows(a).PasteSpecial
      Next
      End Sub


      Thank you alex. i amended the code by reference to yours.
      I subsequently found writing my code like below does not really efficient while there a around 100 worksheets to be copied and 7 master lists to be pasted. Below is the revised code, it seems more efficient to be written by "do if" function? Say once the VBA copied worksheets 2-10 rows 4 to worksheet 1, then start copying worksheets 12-21 rows 4 to worksheet 21



      Sub test2()

      '
      Workbooks.Open Filename:="file location"

      Worksheets(2).Rows("1:3").Copy Destination:=Worksheets(1).Rows(1)
      Worksheets(1).Range("A:AG").ColumnWidth = 20
      Worksheets(1).Range("AD:AD").ColumnWidth = 65
      '
      '
      Worksheets(2).Activate
      a = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

      Set rangeToCopy = Worksheets(2).Range(Cells(4, 1), Cells(a, 33))
      rangeToCopy.Copy
      Dim lastRow As String
      lastRow = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
      Worksheets(1).Cells(lastRow, 1).PasteSpecial ' lastRow,1

      Application.CutCopyMode = False
      Set rangeToCopy = Nothing
      '
      '
      '
      Worksheets(3).Activate
      b = Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row

      Set rangeToCopy = Worksheets(3).Range(Cells(4, 1), Cells(b, 33))
      rangeToCopy.Copy
      Dim lastRow2 As String
      lastRow2 = Worksheets(1).Cells(Rows.Count, "e").End(xlUp).Row + 1
      Worksheets(1).Cells(lastRow2, 1).PasteSpecial ' lastRow2,1

      Application.CutCopyMode = False
      Set rangeToCopy = Nothing

      .....to be continued
      End Sub






      excel vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 4:23







      KC Moses Lai

















      asked Nov 21 '18 at 9:24









      KC Moses LaiKC Moses Lai

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It's easier to copy-insert Range data without any loops, like this:



          Set rangeToCopy= worksheets(1).Range(Cells(1, 1), Cells(a, 100))'insert number of columns instead of 100
          rangeToCopy.Copy
          worksheets(2).Cells(1, 1).PasteSpecial Paste:=xlPasteValues' 1,1 - row, column where u want to insert your data





          share|improve this answer
























          • Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

            – KC Moses Lai
            Nov 22 '18 at 4:24













          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%2f53408847%2fhow-can-i-copy-specific-rows-from-several-worksheets-to-another-with-excel-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          It's easier to copy-insert Range data without any loops, like this:



          Set rangeToCopy= worksheets(1).Range(Cells(1, 1), Cells(a, 100))'insert number of columns instead of 100
          rangeToCopy.Copy
          worksheets(2).Cells(1, 1).PasteSpecial Paste:=xlPasteValues' 1,1 - row, column where u want to insert your data





          share|improve this answer
























          • Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

            – KC Moses Lai
            Nov 22 '18 at 4:24


















          0














          It's easier to copy-insert Range data without any loops, like this:



          Set rangeToCopy= worksheets(1).Range(Cells(1, 1), Cells(a, 100))'insert number of columns instead of 100
          rangeToCopy.Copy
          worksheets(2).Cells(1, 1).PasteSpecial Paste:=xlPasteValues' 1,1 - row, column where u want to insert your data





          share|improve this answer
























          • Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

            – KC Moses Lai
            Nov 22 '18 at 4:24
















          0












          0








          0







          It's easier to copy-insert Range data without any loops, like this:



          Set rangeToCopy= worksheets(1).Range(Cells(1, 1), Cells(a, 100))'insert number of columns instead of 100
          rangeToCopy.Copy
          worksheets(2).Cells(1, 1).PasteSpecial Paste:=xlPasteValues' 1,1 - row, column where u want to insert your data





          share|improve this answer













          It's easier to copy-insert Range data without any loops, like this:



          Set rangeToCopy= worksheets(1).Range(Cells(1, 1), Cells(a, 100))'insert number of columns instead of 100
          rangeToCopy.Copy
          worksheets(2).Cells(1, 1).PasteSpecial Paste:=xlPasteValues' 1,1 - row, column where u want to insert your data






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 12:33









          Alexey CAlexey C

          1356




          1356













          • Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

            – KC Moses Lai
            Nov 22 '18 at 4:24





















          • Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

            – KC Moses Lai
            Nov 22 '18 at 4:24



















          Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

          – KC Moses Lai
          Nov 22 '18 at 4:24







          Thank you Alex. I amended the code by reference to yours. But i still have questions, could you please take a look on the revised code and give me a hand again?

          – KC Moses Lai
          Nov 22 '18 at 4:24






















          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%2f53408847%2fhow-can-i-copy-specific-rows-from-several-worksheets-to-another-with-excel-vba%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

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?