Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-使用.cells属性将范围指定给变量错误,_Excel_Vba_Range_Cells_Variant - Fatal编程技术网

Excel VBA-使用.cells属性将范围指定给变量错误,

Excel VBA-使用.cells属性将范围指定给变量错误,,excel,vba,range,cells,variant,Excel,Vba,Range,Cells,Variant,我有下面的代码摘录 Dim profileRange as Variant profileRange = ActiveWorkbook.Worksheets("Scenario").Range(Cells(1, "C"), Cells(5, "C")) 然后我在手表表达式中得到以下结果 Watch Expression: profileRange Value: Empty Type: Variant/Empty 它应该得到那张表上的数字,从1到5 另外,我在运行代码时也会得到这个结果 Er

我有下面的代码摘录

Dim profileRange as Variant

profileRange = ActiveWorkbook.Worksheets("Scenario").Range(Cells(1, "C"), Cells(5, "C"))
然后我在手表表达式中得到以下结果

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块与
一起使用,可以在整个过程中实现一致的父级关系。

嘿,斯科特,我如何将其分配给profileRange?@lukieleetronic抱歉,忘记了数组。我修好了。请考虑正确的标记。