您收到错误的原因是因为您试图定义一个范围wksDest
using Cells
来自Active工作表,除非wksDest
活跃。所以这个声明:
Set rngSource2 = wksDest.Range(Cells(38, 4), Cells(40, 4))
其实是这么说的:
Set rngSource2 = wksDest.Range(ActiveSheet.Cells(38, 4), ActiveSheet.Cells(40, 4))
由于无法使用另一张工作表上的单元格在一张工作表上定义范围(不采取其他特殊措施),为了避免这种情况,您可以使用With
像这样阻止:
With wksDest
Set rngSource2 = .Range(.Cells(38, 4), .Cells(40, 4))
End With
With wksSource
Set rngDest2 = .Range(.Cells(29, 3 + i - 1), .Cells(31, 3 + i - 1))
End With
或者使用更直接的方法Resize
method:
Set rngSource2 = wksDest.Cells(38,4).Resize(3)
Set rngDest2 = wksSource.Cells(29, 3 + i - 1).Resize(3)
上述两种方法都避免了不合格的情况Cells
对象,这可能是 1004 错误的原因。
另外,由于您只是复制值,因此您可以进行简单的值分配,而不是copy
and pastespecial
,像这样:
rngDest2.Value = rngSource2.Value