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抱歉,忘记了数组。我修好了。请考虑正确的标记。