【问题标题】:Excel VBA: How to copy row from one worksheet to another based on data range with additional criteria to narrow down results?Excel VBA:如何根据具有附加条件的数据范围将行从一个工作表复制到另一个工作表以缩小结果范围?
【发布时间】:2018-06-27 10:19:34
【问题描述】:

我有以下代码,根据 B 列中的日期范围将行从一个工作表复制到另一个工作表。但是,努力添加第二个条件,该条件将消除基于包含文本“操作”的另一列 (C) 的结果。有人可以帮忙吗?

Private Sub CommandButton3_Click()
    Dim startdate As Date, enddate As Date
    Dim rng As Range, destRow As Long
    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim c As Range

    Set shtSrc = Sheets("Tier2")
    Set shtDest = Sheets("Parameters")

    destRow = 74 

    startdate = DateSerial(Year(Now), Month(Now), 1)
    enddate = DateSerial(Year(Now), Month(Now) + 3, 0)


    Set rng = Application.Intersect(shtSrc.Range("B:B"), shtSrc.UsedRange)


    For Each c In rng.Cells
        If c.Value >= startdate And c.Value <= enddate Then

            c.Offset(0, 1).Resize(1, 10).Copy _
                          shtDest.Cells(destRow, 1)

            destRow = destRow + 1

        End If
    Next

End Sub

【问题讨论】:

    标签: excel vba multiple-columns


    【解决方案1】:

    使用偏移量比较 C 列中的数据。
    If 语句中添加以下条件,并附加And

    For Each c In rng.Cells
      If c.Value >= startdate And c.Value <= enddate And c.Offset(0, 1) <> "Action" Then
         '....code.... 
      End If
    Next c 
    

    【讨论】:

    • 没什么好羞愧的,有时最简单的解决方案是我们需要最长时间才能实现的解决方案;)
    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多