简体   繁体   English

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

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

I've the following code extract below, 我在下面提取了以下代码,

Dim profileRange as Variant

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

Then I get the following in the watch expression 然后在watch表达式中得到以下内容

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

It's supposed to get the numbers assigned in that sheet..which goes form 1 to 5 它应该获取该工作表中分配的数字..从1到5

Also, I get this when running the code 另外,我在运行代码时得到了这个

Error 1004:
Application or Object defined error

Can someone please help? 有人可以帮忙吗?

Change: 更改:

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

to this: 对此:

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

Cells() are range object and need to have parentage assigned. Cells()是范围对象,需要分配父项。 Without dictating parentage it is trying to use the active sheet, so the range parentage does not equate to the cells parentage. 在不指定父项的情况下,它尝试使用活动表,因此范围父项不等于单元格的父项。

By using the With block with the . 通过将With块与一起使用. allows consistent parentage throughout. 允许始终一致的父母身份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM