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




























    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


























      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















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 10:29









      Pᴇʜ

      19.7k42650




      19.7k42650










      asked Nov 19 at 9:07









      Hannah Nev

      133




      133
























          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
            Nov 19 at 10:31










          • That worked! Thanks so much!!
            – Hannah Nev
            Nov 19 at 10:33










          • 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
            Nov 19 at 10:49




















          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




























            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
              });


              }
              });














              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
                Nov 19 at 10:31










              • That worked! Thanks so much!!
                – Hannah Nev
                Nov 19 at 10:33










              • 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
                Nov 19 at 10:49

















              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
                Nov 19 at 10:31










              • That worked! Thanks so much!!
                – Hannah Nev
                Nov 19 at 10:33










              • 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
                Nov 19 at 10:49















              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 Nov 19 at 10:28









              SINAN NIZAR

              334




              334












              • Please provide some comments and explanation as to why what you have posted is an answer.
                – Andy G
                Nov 19 at 10:31










              • That worked! Thanks so much!!
                – Hannah Nev
                Nov 19 at 10:33










              • 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
                Nov 19 at 10:49




















              • Please provide some comments and explanation as to why what you have posted is an answer.
                – Andy G
                Nov 19 at 10:31










              • That worked! Thanks so much!!
                – Hannah Nev
                Nov 19 at 10:33










              • 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
                Nov 19 at 10:49


















              Please provide some comments and explanation as to why what you have posted is an answer.
              – Andy G
              Nov 19 at 10:31




              Please provide some comments and explanation as to why what you have posted is an answer.
              – Andy G
              Nov 19 at 10:31












              That worked! Thanks so much!!
              – Hannah Nev
              Nov 19 at 10:33




              That worked! Thanks so much!!
              – Hannah Nev
              Nov 19 at 10:33












              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
              Nov 19 at 10:49






              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
              Nov 19 at 10:49














              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

























                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























                  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












                  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












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 10:55









                  Almantas Bendoraitis

                  112




                  112






















                      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 Nov 19 at 12:04

























                          answered Nov 19 at 10:35









                          DirtyDeffy

                          3909




                          3909






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%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

                              If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                              Alcedinidae

                              Can an atomic nucleus contain both particles and antiparticles? [duplicate]