我有遍历单元格范围的循环,如果满足条件,则将相关数据复制到一张纸上,如果不满足,则复制到另一张纸上。 动态使用范围进行复制时出现问题。
我尝试以2种不同的方式引用该范围,但得到相同的错误
For i = 2 To lastRow
Set found= WC.Range("O1:O" & LastInList).Find(SL.Range("G" & i))
If Not (found Is Nothing)
SL.Range(Cells(i, 1), Cells(i, 3)).Copy Destination:=WC.Range("A" & WCEmptyRow) 'ERROR here
Else
'do something
End If
Next i
也尝试过:
SL.Range("A" & i, "C" & i).Copy Destination:=WC.Range("A" & WCEmptyRow) ' ERROR here
我应该如何引用第I行中的单元格以进行复制?
答案 0 :(得分:1)
什么是foundAH
?它不在代码中。确保tahat支票用于If Not (foundAH Is Nothing)
。为了避免未声明的变量,请从注释中遵循@PEH的Option Explicit
-在VBA编辑器中,转到工具›选项›要求变量声明
此外,代码位于number 1 error in the VBA tag in SO - not explicitly declaring parent worksheets中。请参见修改后的代码中的点:
For i = 2 To lastRow
Set found= WC.Range("O1:O" & LastInList).Find(SL.Range("G" & i))
If Not (found Is Nothing)
With SL
.Range(.Cells(i, 1), .Cells(i, 3)).Copy Destination:=WC.Range("A" & WCEmptyRow)
End With
Else
'do something
End If
Next i