1

有点奇怪的标题,我会解释我在问什么。

使用.Range("A1:B2").copyvs 使用.Range(Cells(1, 1), Cells(2, 2)).copy

两者都可以,但只有当我在实际工作表上时。如果调用宏时我在另一张纸上,则只有字母数字范围有效。

有问题的实际代码:

CurrentExtractSheet.Range("A2: AR" & (CurrentExtractCount + 1)).Copy
PreviousExtractSheet.Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

对比

CurrentExtractSheet.Range(Cells(2, 1), Cells(CurrentExtractCount + 1, 45)).Copy
PreviousExtractSheet.Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

那么我要问的是有什么办法可以解决这个问题(我更喜欢使用该Cells方法而不是字母数字范围)?是否Cells首先需要一个活动表(我也不特别喜欢)?

提前致谢。

4

2 回答 2

4

您不需要将 CurrentExtractSheet 设置为 ActiveSheet,但您确实需要将其包含在您的 Cells 命令中......

CurrentExtractSheet.Range(CurrentExtractSheet.Cells(2, 1), CurrentExtractSheet.Cells(CurrentExtractCount + 1, 45)).Copy

或者

With CurrentExtractSheet
 .Range(.Cells(2, 1),.Cells(CurrentExtractCount + 1, 45)).Copy
End With
于 2013-10-28T04:21:17.937 回答
3

When doing Cells(x,y) without specifying a sheet, your are in fact referening to the activesheet.

So, CurrentExtractSheet.Range(Cells(2, 1), Cells(CurrentExtractCount + 1, 45)) is equivalent to CurrentExtractSheet.Range(Activesheet.Cells(2, 1), Activesheet.Cells(CurrentExtractCount + 1, 45))

When CurrentExtractSheet is not the active sheet it fails.

There are several ways to avoid this error

With CurrentExtractSheet
    Range(.Cells(2, 1), .Cells(CurrentExtractCount + 1, 45)) ...
End With

Note, when you specify the sheets for the two .Cells references you don't need to qualift Range

Another otpion

CurrentExtractSheet.Cells(2, 1).Resize(CurrentExtractCount, 45) ...
于 2013-10-28T04:25:47.243 回答