copy row from one excel sheet to another based on 2 criteria inputs











up vote
2
down vote

favorite












I have an excel sheet where the first sheet contains all of the master data which will be around 500 rows and goes across to column R, this sheet is called Overall Sheet. Column O includes a month, and Column P includes a year.



I have another sheet where I would like the data to be copied this is called "Forecast Month". at the top in B1, the month that I would like to be copied in is selected, and in D1, the year is selected. I would like the button to read these two cells and copy in the data from "overall sheet" based on this.



I have written this code as shown below, but for some reason the data is entered into "forecast month" 10 times before adding the next one (also 10 times). I should only have 3 pieces of data in this sheet but instead there is 30, 10 for each.



Also the top 3 lines on each sheet have headings so the data should start writing on row 4 (which it does)



Please can anybody help??



Private Sub CommandButton1_Click()
Dim month As String
Dim year As String

Dim c As Range
Dim d As Range

Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet

'change worksheet designations as needed
Set source = ActiveWorkbook.Worksheets("Overall Sheet")
Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

targetforecastmonth.Range("A4:Z1000").Clear

month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

k = 4

For Each c In source.Range("O4:O1000")
For Each d In source.Range("P4:P1000")
If c = month And d = year Then
source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
k = k + 1
End If
Next d
Next c
End Sub









share|improve this question









New contributor




Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    I have an excel sheet where the first sheet contains all of the master data which will be around 500 rows and goes across to column R, this sheet is called Overall Sheet. Column O includes a month, and Column P includes a year.



    I have another sheet where I would like the data to be copied this is called "Forecast Month". at the top in B1, the month that I would like to be copied in is selected, and in D1, the year is selected. I would like the button to read these two cells and copy in the data from "overall sheet" based on this.



    I have written this code as shown below, but for some reason the data is entered into "forecast month" 10 times before adding the next one (also 10 times). I should only have 3 pieces of data in this sheet but instead there is 30, 10 for each.



    Also the top 3 lines on each sheet have headings so the data should start writing on row 4 (which it does)



    Please can anybody help??



    Private Sub CommandButton1_Click()
    Dim month As String
    Dim year As String

    Dim c As Range
    Dim d As Range

    Dim k As Integer
    Dim source As Worksheet
    Dim targetforecastmonth As Worksheet

    'change worksheet designations as needed
    Set source = ActiveWorkbook.Worksheets("Overall Sheet")
    Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

    targetforecastmonth.Range("A4:Z1000").Clear

    month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
    year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

    k = 4

    For Each c In source.Range("O4:O1000")
    For Each d In source.Range("P4:P1000")
    If c = month And d = year Then
    source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
    k = k + 1
    End If
    Next d
    Next c
    End Sub









    share|improve this question









    New contributor




    Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have an excel sheet where the first sheet contains all of the master data which will be around 500 rows and goes across to column R, this sheet is called Overall Sheet. Column O includes a month, and Column P includes a year.



      I have another sheet where I would like the data to be copied this is called "Forecast Month". at the top in B1, the month that I would like to be copied in is selected, and in D1, the year is selected. I would like the button to read these two cells and copy in the data from "overall sheet" based on this.



      I have written this code as shown below, but for some reason the data is entered into "forecast month" 10 times before adding the next one (also 10 times). I should only have 3 pieces of data in this sheet but instead there is 30, 10 for each.



      Also the top 3 lines on each sheet have headings so the data should start writing on row 4 (which it does)



      Please can anybody help??



      Private Sub CommandButton1_Click()
      Dim month As String
      Dim year As String

      Dim c As Range
      Dim d As Range

      Dim k As Integer
      Dim source As Worksheet
      Dim targetforecastmonth As Worksheet

      'change worksheet designations as needed
      Set source = ActiveWorkbook.Worksheets("Overall Sheet")
      Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

      targetforecastmonth.Range("A4:Z1000").Clear

      month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
      year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

      k = 4

      For Each c In source.Range("O4:O1000")
      For Each d In source.Range("P4:P1000")
      If c = month And d = year Then
      source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
      k = k + 1
      End If
      Next d
      Next c
      End Sub









      share|improve this question









      New contributor




      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have an excel sheet where the first sheet contains all of the master data which will be around 500 rows and goes across to column R, this sheet is called Overall Sheet. Column O includes a month, and Column P includes a year.



      I have another sheet where I would like the data to be copied this is called "Forecast Month". at the top in B1, the month that I would like to be copied in is selected, and in D1, the year is selected. I would like the button to read these two cells and copy in the data from "overall sheet" based on this.



      I have written this code as shown below, but for some reason the data is entered into "forecast month" 10 times before adding the next one (also 10 times). I should only have 3 pieces of data in this sheet but instead there is 30, 10 for each.



      Also the top 3 lines on each sheet have headings so the data should start writing on row 4 (which it does)



      Please can anybody help??



      Private Sub CommandButton1_Click()
      Dim month As String
      Dim year As String

      Dim c As Range
      Dim d As Range

      Dim k As Integer
      Dim source As Worksheet
      Dim targetforecastmonth As Worksheet

      'change worksheet designations as needed
      Set source = ActiveWorkbook.Worksheets("Overall Sheet")
      Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

      targetforecastmonth.Range("A4:Z1000").Clear

      month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
      year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

      k = 4

      For Each c In source.Range("O4:O1000")
      For Each d In source.Range("P4:P1000")
      If c = month And d = year Then
      source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
      k = k + 1
      End If
      Next d
      Next c
      End Sub






      excel vba performance optimization copy-paste






      share|improve this question









      New contributor




      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 5 hours ago









      Pᴇʜ

      18.7k42549




      18.7k42549






      New contributor




      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      Hannah Nev

      133




      133




      New contributor




      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Hannah Nev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Try this, I hope this would help.



          Private Sub CommandButton1_Click()
          Dim month As String
          Dim year As String

          Dim c As Range

          Dim k As Integer
          Dim source As Worksheet
          Dim targetforecastmonth As Worksheet

          'change worksheet designations as needed
          Set source = ActiveWorkbook.Worksheets("Overall Sheet")
          Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

          targetforecastmonth.Range("A4:Z1000").Clear

          month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
          year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

          k = 4

          For Each c In source.Range("O4:O1000")
          If c = month And source.Cells(c.Row, 16).Value = year Then
          source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
          k = k + 1
          End If
          Next c

          End Sub





          share|improve this answer





















          • Please provide some comments and explanation as to why what you have posted is an answer.
            – Andy G
            5 hours ago










          • That worked! Thanks so much!!
            – Hannah Nev
            5 hours ago










          • I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
            – SINAN NIZAR
            4 hours ago




















          up vote
          1
          down vote













          It seems wrong logic there is.
          I suppose you need Expl.: O8, P8 matches B1, D1
          So you need only one cycle:



          For Each c In source.Range("O4:O1000")
          d = source.Range("P" & k)
          If c = month And d = year Then
          source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
          End If
          k = k + 1
          Next c





          share|improve this answer








          New contributor




          Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

























            up vote
            0
            down vote













            You have a nested For Each loop, meaning that you take cell "O4" then loop through cells "P4:P1000" before moving on to cell "O5" and looping through cells "P4:P1000" again and so forth... If for example the cell value of "O4" satisfies the month criteria, then every time the loop through column P finds a cell that satisfies the year criteria, row number 4 will be copied and pasted.
            Try this instead:



            Private Sub CommandButton1_Click()
            Dim month As String
            Dim year As String

            Dim c As Range
            Dim d As Range
            Dim x As Long

            Dim k As Integer
            Dim source As Worksheet
            Dim targetforecastmonth As Worksheet

            'change worksheet designations as needed
            Set source = ActiveWorkbook.Worksheets("Overall Sheet")
            Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

            targetforecastmonth.Range("A4:Z1000").Clear

            month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
            year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

            k = 4
            x = 4

            For Each c In source.Range("O4:O1000")
            Set d = source.Range("P" & x)
            If c.Value = month And d.Value = year Then
            source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
            k = k + 1
            End If
            x = x + 1
            Next c
            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',
              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
              });


              }
              });






              Hannah Nev is a new contributor. Be nice, and check out our Code of Conduct.










               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371332%2fcopy-row-from-one-excel-sheet-to-another-based-on-2-criteria-inputs%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








              up vote
              0
              down vote



              accepted










              Try this, I hope this would help.



              Private Sub CommandButton1_Click()
              Dim month As String
              Dim year As String

              Dim c As Range

              Dim k As Integer
              Dim source As Worksheet
              Dim targetforecastmonth As Worksheet

              'change worksheet designations as needed
              Set source = ActiveWorkbook.Worksheets("Overall Sheet")
              Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

              targetforecastmonth.Range("A4:Z1000").Clear

              month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
              year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

              k = 4

              For Each c In source.Range("O4:O1000")
              If c = month And source.Cells(c.Row, 16).Value = year Then
              source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
              k = k + 1
              End If
              Next c

              End Sub





              share|improve this answer





















              • Please provide some comments and explanation as to why what you have posted is an answer.
                – Andy G
                5 hours ago










              • That worked! Thanks so much!!
                – Hannah Nev
                5 hours ago










              • I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
                – SINAN NIZAR
                4 hours ago

















              up vote
              0
              down vote



              accepted










              Try this, I hope this would help.



              Private Sub CommandButton1_Click()
              Dim month As String
              Dim year As String

              Dim c As Range

              Dim k As Integer
              Dim source As Worksheet
              Dim targetforecastmonth As Worksheet

              'change worksheet designations as needed
              Set source = ActiveWorkbook.Worksheets("Overall Sheet")
              Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

              targetforecastmonth.Range("A4:Z1000").Clear

              month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
              year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

              k = 4

              For Each c In source.Range("O4:O1000")
              If c = month And source.Cells(c.Row, 16).Value = year Then
              source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
              k = k + 1
              End If
              Next c

              End Sub





              share|improve this answer





















              • Please provide some comments and explanation as to why what you have posted is an answer.
                – Andy G
                5 hours ago










              • That worked! Thanks so much!!
                – Hannah Nev
                5 hours ago










              • I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
                – SINAN NIZAR
                4 hours ago















              up vote
              0
              down vote



              accepted







              up vote
              0
              down vote



              accepted






              Try this, I hope this would help.



              Private Sub CommandButton1_Click()
              Dim month As String
              Dim year As String

              Dim c As Range

              Dim k As Integer
              Dim source As Worksheet
              Dim targetforecastmonth As Worksheet

              'change worksheet designations as needed
              Set source = ActiveWorkbook.Worksheets("Overall Sheet")
              Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

              targetforecastmonth.Range("A4:Z1000").Clear

              month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
              year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

              k = 4

              For Each c In source.Range("O4:O1000")
              If c = month And source.Cells(c.Row, 16).Value = year Then
              source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
              k = k + 1
              End If
              Next c

              End Sub





              share|improve this answer












              Try this, I hope this would help.



              Private Sub CommandButton1_Click()
              Dim month As String
              Dim year As String

              Dim c As Range

              Dim k As Integer
              Dim source As Worksheet
              Dim targetforecastmonth As Worksheet

              'change worksheet designations as needed
              Set source = ActiveWorkbook.Worksheets("Overall Sheet")
              Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

              targetforecastmonth.Range("A4:Z1000").Clear

              month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
              year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

              k = 4

              For Each c In source.Range("O4:O1000")
              If c = month And source.Cells(c.Row, 16).Value = year Then
              source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
              k = k + 1
              End If
              Next c

              End Sub






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 5 hours ago









              SINAN NIZAR

              182




              182












              • Please provide some comments and explanation as to why what you have posted is an answer.
                – Andy G
                5 hours ago










              • That worked! Thanks so much!!
                – Hannah Nev
                5 hours ago










              • I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
                – SINAN NIZAR
                4 hours ago




















              • Please provide some comments and explanation as to why what you have posted is an answer.
                – Andy G
                5 hours ago










              • That worked! Thanks so much!!
                – Hannah Nev
                5 hours ago










              • I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
                – SINAN NIZAR
                4 hours ago


















              Please provide some comments and explanation as to why what you have posted is an answer.
              – Andy G
              5 hours ago




              Please provide some comments and explanation as to why what you have posted is an answer.
              – Andy G
              5 hours ago












              That worked! Thanks so much!!
              – Hannah Nev
              5 hours ago




              That worked! Thanks so much!!
              – Hannah Nev
              5 hours ago












              I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
              – SINAN NIZAR
              4 hours ago






              I'm glad it worked :) You were looping the rows multiple times and that caused the error. Instead of a single loop you used a nested one, and that's why you had multiple lines printed.
              – SINAN NIZAR
              4 hours ago














              up vote
              1
              down vote













              It seems wrong logic there is.
              I suppose you need Expl.: O8, P8 matches B1, D1
              So you need only one cycle:



              For Each c In source.Range("O4:O1000")
              d = source.Range("P" & k)
              If c = month And d = year Then
              source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
              End If
              k = k + 1
              Next c





              share|improve this answer








              New contributor




              Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                1
                down vote













                It seems wrong logic there is.
                I suppose you need Expl.: O8, P8 matches B1, D1
                So you need only one cycle:



                For Each c In source.Range("O4:O1000")
                d = source.Range("P" & k)
                If c = month And d = year Then
                source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                End If
                k = k + 1
                Next c





                share|improve this answer








                New contributor




                Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  It seems wrong logic there is.
                  I suppose you need Expl.: O8, P8 matches B1, D1
                  So you need only one cycle:



                  For Each c In source.Range("O4:O1000")
                  d = source.Range("P" & k)
                  If c = month And d = year Then
                  source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                  End If
                  k = k + 1
                  Next c





                  share|improve this answer








                  New contributor




                  Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  It seems wrong logic there is.
                  I suppose you need Expl.: O8, P8 matches B1, D1
                  So you need only one cycle:



                  For Each c In source.Range("O4:O1000")
                  d = source.Range("P" & k)
                  If c = month And d = year Then
                  source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                  End If
                  k = k + 1
                  Next c






                  share|improve this answer








                  New contributor




                  Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 4 hours ago









                  Almantas Bendoraitis

                  112




                  112




                  New contributor




                  Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Almantas Bendoraitis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                      up vote
                      0
                      down vote













                      You have a nested For Each loop, meaning that you take cell "O4" then loop through cells "P4:P1000" before moving on to cell "O5" and looping through cells "P4:P1000" again and so forth... If for example the cell value of "O4" satisfies the month criteria, then every time the loop through column P finds a cell that satisfies the year criteria, row number 4 will be copied and pasted.
                      Try this instead:



                      Private Sub CommandButton1_Click()
                      Dim month As String
                      Dim year As String

                      Dim c As Range
                      Dim d As Range
                      Dim x As Long

                      Dim k As Integer
                      Dim source As Worksheet
                      Dim targetforecastmonth As Worksheet

                      'change worksheet designations as needed
                      Set source = ActiveWorkbook.Worksheets("Overall Sheet")
                      Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

                      targetforecastmonth.Range("A4:Z1000").Clear

                      month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
                      year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

                      k = 4
                      x = 4

                      For Each c In source.Range("O4:O1000")
                      Set d = source.Range("P" & x)
                      If c.Value = month And d.Value = year Then
                      source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                      k = k + 1
                      End If
                      x = x + 1
                      Next c
                      End Sub





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        You have a nested For Each loop, meaning that you take cell "O4" then loop through cells "P4:P1000" before moving on to cell "O5" and looping through cells "P4:P1000" again and so forth... If for example the cell value of "O4" satisfies the month criteria, then every time the loop through column P finds a cell that satisfies the year criteria, row number 4 will be copied and pasted.
                        Try this instead:



                        Private Sub CommandButton1_Click()
                        Dim month As String
                        Dim year As String

                        Dim c As Range
                        Dim d As Range
                        Dim x As Long

                        Dim k As Integer
                        Dim source As Worksheet
                        Dim targetforecastmonth As Worksheet

                        'change worksheet designations as needed
                        Set source = ActiveWorkbook.Worksheets("Overall Sheet")
                        Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

                        targetforecastmonth.Range("A4:Z1000").Clear

                        month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
                        year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

                        k = 4
                        x = 4

                        For Each c In source.Range("O4:O1000")
                        Set d = source.Range("P" & x)
                        If c.Value = month And d.Value = year Then
                        source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                        k = k + 1
                        End If
                        x = x + 1
                        Next c
                        End Sub





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You have a nested For Each loop, meaning that you take cell "O4" then loop through cells "P4:P1000" before moving on to cell "O5" and looping through cells "P4:P1000" again and so forth... If for example the cell value of "O4" satisfies the month criteria, then every time the loop through column P finds a cell that satisfies the year criteria, row number 4 will be copied and pasted.
                          Try this instead:



                          Private Sub CommandButton1_Click()
                          Dim month As String
                          Dim year As String

                          Dim c As Range
                          Dim d As Range
                          Dim x As Long

                          Dim k As Integer
                          Dim source As Worksheet
                          Dim targetforecastmonth As Worksheet

                          'change worksheet designations as needed
                          Set source = ActiveWorkbook.Worksheets("Overall Sheet")
                          Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

                          targetforecastmonth.Range("A4:Z1000").Clear

                          month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
                          year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

                          k = 4
                          x = 4

                          For Each c In source.Range("O4:O1000")
                          Set d = source.Range("P" & x)
                          If c.Value = month And d.Value = year Then
                          source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                          k = k + 1
                          End If
                          x = x + 1
                          Next c
                          End Sub





                          share|improve this answer














                          You have a nested For Each loop, meaning that you take cell "O4" then loop through cells "P4:P1000" before moving on to cell "O5" and looping through cells "P4:P1000" again and so forth... If for example the cell value of "O4" satisfies the month criteria, then every time the loop through column P finds a cell that satisfies the year criteria, row number 4 will be copied and pasted.
                          Try this instead:



                          Private Sub CommandButton1_Click()
                          Dim month As String
                          Dim year As String

                          Dim c As Range
                          Dim d As Range
                          Dim x As Long

                          Dim k As Integer
                          Dim source As Worksheet
                          Dim targetforecastmonth As Worksheet

                          'change worksheet designations as needed
                          Set source = ActiveWorkbook.Worksheets("Overall Sheet")
                          Set targetforecastmonth = ActiveWorkbook.Worksheets("Forecast Month")

                          targetforecastmonth.Range("A4:Z1000").Clear

                          month = ActiveWorkbook.Worksheets("Forecast Month").Range("B1")
                          year = ActiveWorkbook.Worksheets("Forecast Month").Range("D1")

                          k = 4
                          x = 4

                          For Each c In source.Range("O4:O1000")
                          Set d = source.Range("P" & x)
                          If c.Value = month And d.Value = year Then
                          source.Rows(c.Row).Copy targetforecastmonth.Rows(k)
                          k = k + 1
                          End If
                          x = x + 1
                          Next c
                          End Sub






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 3 hours ago

























                          answered 5 hours ago









                          DirtyDeffy

                          3909




                          3909






















                              Hannah Nev is a new contributor. Be nice, and check out our Code of Conduct.










                               

                              draft saved


                              draft discarded


















                              Hannah Nev is a new contributor. Be nice, and check out our Code of Conduct.













                              Hannah Nev is a new contributor. Be nice, and check out our Code of Conduct.












                              Hannah Nev is a new contributor. Be nice, and check out our Code of Conduct.















                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371332%2fcopy-row-from-one-excel-sheet-to-another-based-on-2-criteria-inputs%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

                              'app-layout' is not a known element: how to share Component with different Modules

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              WPF add header to Image with URL pettitions [duplicate]