尝试将array(I,1).value分配给暗淡的字符串类型变量时出现错误424 VBA

时间:2019-06-25 02:56:51

标签: excel vba

此代码给我错误424。请问我该如何解决?

Dim a As String
Dim rRange() As Variant
rRange = wb.sheet("sheetname").Range("A1:A5").Value2

现在我想为变量分配一个值

a = rRange(I, 1).value

1 个答案:

答案 0 :(得分:0)

有问题的方程式的右侧不是Excel Range对象,而是二维数组。数组没有方法和函数(它们不是对象或类)。

一些关于您原始代码的评论:

Dim a As String
Dim rRange() As Variant
rRange = wb.sheet("sheetname").Range("A1:A5").Value2 '<- This is where the 2-D variant array is set.

Now i want to assign a value into a variable
For I = <something to something> ' added this for some context
    a = rRange(I, 1).value ' <- ".value" is not applicable to a variant array
next I ' added this for some context

您的代码可能是:

Option Explicit
Dim a As String
Dim rRangeValues As Variant '<- () not really required, but in robust code should check for a single value

rRangeValues = wb.sheet("sheetname").Range("A1:A5").Value2 '<- This is where the 2-D variant array is set.

For I = LBound(rRangeValues, 1) to UBound(rRangeValues, 1) 
    a = rRangeValues(I, 1)
    '<Do something with the value>
next I