'If Then' 循环复制和粘贴
'If Then' loop with copy and 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,而是执行以下操作:
-select 所有通过这两个条件的行,并将它们的值从 Sheet1 复制并粘贴到 Sheet2
我认为复制和粘贴部分会非常简单,但我不确定根据 if...then 语句的结果 select 不同行的最佳方法。
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
我目前有一小部分 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,而是执行以下操作:
-select 所有通过这两个条件的行,并将它们的值从 Sheet1 复制并粘贴到 Sheet2
我认为复制和粘贴部分会非常简单,但我不确定根据 if...then 语句的结果 select 不同行的最佳方法。
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