如何在我的代码中引用另一个工作表中的范围?

How do I refer to a range in another worksheet in my code?

这个有效:

Sub exemp2()
Dim frange As Range
Dim x As Integer
x = 1
Set frange = Worksheets("Sheet6").Range("A1")
Worksheets("Sheet5").Activate
Range(Cells(x, 1), Cells(5, 2)).Copy Destination:=frange
Cells(8, 1).Value = ActiveSheet.Name
End Sub

这给出了 1004 错误:

Sub exemp1()
Dim frange As Range
Dim x As Integer
x = 1
Set frange = Worksheets("Sheet6").Range(Worksheets("Sheet6").Cells(1, 1))
Worksheets("Sheet5").Activate
Range(Cells(x, 1), Cells(5, 2)).Copy Destination:=frange
Cells(8, 1).Value = ActiveSheet.Name
End Sub

我不明白为什么我不能像第二个例子那样引用范围。

Workseet.Range属性[=]的第一个参数Cell1(标记为Required)请查MSDN说明如下16=]

The name of the range. This must be an A1-style reference in the language of the macro. It can include the range operator (a colon), the intersection operator (a space), or the union operator (a comma). It can also include dollar signs, but they’re ignored. You can use a local defined name in any part of the range. If you use a name, the name is assumed to be in the language of the macro.

在这里你可以找到complete MSDN article on this subject

解决方法

但是还有另一种选择 - 您可以使用以下调用:

Set frange = Worksheets("Sheet6").Range(Worksheets("Sheet6").Cells(1, 1), _
                                        Worksheets("Sheet6").Cells(1, 1))

而不是

Set frange = Worksheets("Sheet6").Range(Worksheets("Sheet6").Cells(1, 1)

这不会给出任何错误。看起来当一个人想在范围单元格寻址中使用对象引用时,应该给出第二个参数。