Array()= range() [英] Array() = range().value
问题描述
我在一个例子中看到 array()= range()。value
,我正在试图了解它的工作原理。
I saw array() = range().value
in an example and I'm trying to understand how it works.
Sub test()
Dim arr() As Variant
arr() = Range("E5:E7").Value
For i = 0 To UBound(arr)
Debug.Print arr(i)
Next i
End Sub
首先,以上代码给我下标超出范围
错误。怎么来的 ?第二,文档的哪一部分将让我知道如何 array()= range()。value
会在没有测试的情况下播放?我的假设是,它将通过从左上角到右下角的单元格,并将它们添加到数组中。如何确认?
First, above code is giving me subscript out of range
error. How come ? Second, what part of the documentation would let me know how array() = range().value
would play out without testing it ? My hypothesis is that it will go through the cells from the top left to the bottom right and add them to the array. How can I confirm that though ?
推荐答案
这是一个很好的阅读: http://www.cpearson.com/excel/ArraysAndRanges.aspx
This is a good read for you: http://www.cpearson.com/excel/ArraysAndRanges.aspx
你得到超出范围的原因是因为它返回一个二维数组。
The reason you're getting "out of range" is because it returns a 2 dimensional array.
您的代码行对于i = 0对于UBound(arr)
应为对于i = 1 To UBound(arr,1)
另外,数组从1开始,所以不要使用0 对于i = 1到UBound(arr,1)
Also, the array starts at 1, so don't use the 0 For i = 1 to UBound(arr, 1)
您更正的代码将是:
Sub Test()
Dim arr() as Variant
arr = Range("E5:E7")
For i = 1 To UBound(arr, 1)
MsgBox (arr(i, 1))
Next i
End Sub
这篇关于Array()= range()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!