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

Excel VBA - Assigning range to variant using .cells property error,

我在下面摘录了以下代码,

Dim profileRange as Variant

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

然后我在手表表达式中得到以下内容

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

它应该得到分配的数字 sheet..从 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

Cells() 是范围对象,需要分配亲子关系。在不指定出身的情况下,它试图使用活动 sheet,因此范围出身不等同于单元格出身。

通过将 With 块与 . 一起使用,可以在整个过程中保持一致的父系关系。