【问题标题】:copy row from one excel sheet to another based on 2 criteria inputs根据 2 个标准输入将行从一个 Excel 工作表复制到另一个工作表
【发布时间】:2018-11-19 20:04:36
【问题描述】:

我有一张 Excel 表格,其中第一张表格包含所有主数据,大约 500 行并跨越 R 列,这张表格称为整体表格。 O 列包含一个月,P 列包含一年。

我有另一张表,我希望在其中复制数据,这称为“预测月份”。在 B1 的顶部,选择了我要复制的月份,在 D1 中,选择了年份。我希望按钮读取这两个单元格并基于此从“整体工作表”中复制数据。

我已经编写了如下所示的代码,但由于某种原因,在添加下一个(也是 10 次)之前,数据在“预测月份”中输入了 10 次。我应该在这张表中只有 3 条数据,但每个有 30、10 条数据。

此外,每张纸上的前 3 行都有标题,因此数据应该从第 4 行开始写入(确实如此)

请问有人可以帮忙吗?

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


    【解决方案1】:

    这似乎是错误的逻辑。 我想你需要 Expl.: O8, P8 匹配 B1, D1 所以你只需要一个周期:

    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
    

    【讨论】:

      【解决方案2】:

      试试这个,希望对你有帮助。

      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
      

      【讨论】:

      • 请提供一些 cmets 并解释为什么您发布的内容是答案。
      • 我很高兴它起作用了:) 您多次循环这些行并导致错误。您使用嵌套循环而不是单个循环,这就是您打印多行的原因。
      【解决方案3】:

      您有一个嵌套的For Each 循环,这意味着您获取单元格"O4",然后循环遍历单元格"P4:P1000",然后再转到单元格"O5" 并再次循环遍历单元格"P4:P1000",依此类推...如果例如"O4"的单元格值满足month条件,那么每次循环通过列P找到满足year条件的单元格时,将复制并粘贴行号4。 试试这个:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多