'如果然后'循环复制并粘贴

时间:2015-10-12 13:42:31

标签: excel vba loops if-statement copy-paste

我目前有一小部分VBA可以搜索条件并根据条件进行基本添加。效果很好,如下:

Dim i As Long
i = 6
Do While Cells(i, "C").Value <> ""
If Cells(i, "C").Value = "9/12/2015" And Cells(i, "E").Value = "9/13/2015" Then
        Cells(i, "M").Value = Cells(i, "M").Value + 12
End If
i = i + 1
Loop

我现在想把它提升到一个新的水平,而不是根据标准向一个列添加12,请执行以下操作:

- 选择通过这两个条件的所有行,并复制&amp;将其值从Sheet1粘贴到Sheet2

我认为复制和粘贴部分非常简单,但我不确定根据if ... then语句的结果选择不同行的最佳方法。

1 个答案:

答案 0 :(得分:0)

Dim i As Long: i = 6
Dim r as Range

Do While Cells(i, "C").Value <> ""
  If Cells(i, "C").Value = "9/12/2015" And Cells(i, "E").Value = "9/13/2015" Then
    if r is nothing then
      set r = Cells(i, "M")
    else
      set r = application.union(r, Cells(i, "M"))
    end if
  End If

  i = i + 1
Loop

if not r is nothing then
  r.Copy Sheet2.Range("A1")
end if
相关问题