Excel VBA – 使用.cells属性错误将范围分配给变体,

我有下面的代码提取下面,

Dim profileRange as Variant profileRange = ActiveWorkbook.Worksheets("Scenario").Range(Cells(1, "C"), Cells(5, "C")) 

然后在expression式中得到以下内容

 Watch Expression: profileRange Value: Empty Type: Variant/Empty 

它应该得到在该表中分配的数字…从1到5

另外,我在运行代码时得到了这个

 Error 1004: Application or Object defined error 

有人可以帮忙吗?

更改:

 profileRange = ActiveWorkbook.Worksheets("Scenario").Range(Cells(1, "C"), Cells(5, "C")) 

对此:

 With ActiveWorkbook.Worksheets("Scenario") profileRange = .Range(.Cells(1, "C"), .Cells(5, "C")) End with 

单元格()是范围对象,需要分配父项。 没有听写亲子关系,它正在尝试使用活动工作表,所以范围血统不等同于单元格血统。

通过使用With块与. 允许始终如一的亲子关系。