I have declared an array as such Dim rArray() As Variant
but when i try and use the values that is stored in it (as shown below) I get a subscript out of range error. The UBound(rArray)
and LBound(rArray)
both returns values 14 and 1, but the error occurs at the Debug.Print
line.
我将一个数组声明为Dim rArray()作为变量,但是当我尝试使用其中存储的值(如下所示)时,会得到一个超出范围的下标错误。UBound(rArray)和LBound(rArray)都返回值14和1,但错误发生在调试过程中。打印行。
If I use the for statement as below
如果我使用下面的for语句。
For Each rArr in rArray
then it works without issues, but for the purposes I am creating this array I need the flexibility to select each item stored in that order- meaning I need to refer to them using subscripts.
然后它就可以毫无问题地工作了,但是为了创建这个数组,我需要灵活地选择按这个顺序存储的每个条目——这意味着我需要使用下标来引用它们。
I have tried multiple ways to try and solve this with no luck and spend almost half my day on this one issue. Could anyone point out what I need to change to get this to work.
我尝试了多种方法来解决这个问题,但运气不好,我几乎花了半天的时间在这个问题上。谁能指出我需要改变什么来让这个工作。
Set rng = Range("D4", Range("D4").End(xlDown))
rng.NumberFormat = "0"
rArray = rng.Value
For x = UBound(rArray) To LBound(rArray) Step -1
Debug.Print rArray(x)
Next x
Edit: another fact worth mentioning is that he array is declared and used within a Function but it is not passed from or to the function. Can't arrays be declared and used in Functions?
编辑:另一个值得一提的事实是,数组是在函数中声明和使用的,但并没有从函数中传递给函数。数组不能在函数中声明和使用吗?
1 个解决方案
#1
7
When you assign worksheet values to a variant array, you always end up with a 2-D array that is 1 based (e.g. 1 to something, 1 to something; never 0 to something, 0 to something). If you are getting values from a single column the second Rank is merely 1 to 1.
当您将工作表值分配给一个变量数组时,您总是会得到一个基于1的二维数组(例如:1到某物,1到某物;从0到某物,从0到某物)如果你从一列中得到值,第二列仅仅是1到1。
This can be proven with the following.
可以用以下方法来证明这一点。
Dim x As Long, rArray As Variant, rng As Range
Set rng = Range("D4", Range("D4").End(xlDown))
rng.NumberFormat = "0" 'don't really understand why this is here
rArray = rng.Value
Debug.Print LBound(rArray, 1) & ":" & UBound(rArray, 1)
Debug.Print LBound(rArray, 2) & ":" & UBound(rArray, 2)
For x = UBound(rArray, 1) To LBound(rArray, 1) Step -1
Debug.Print rArray(x, 1)
Next x
So you need to ask for the element in the first rank of the array; it is insufficient to just ask for the element.
所以你需要要求数组第一级的元素;仅仅要求元素是不够的。
#1
7
When you assign worksheet values to a variant array, you always end up with a 2-D array that is 1 based (e.g. 1 to something, 1 to something; never 0 to something, 0 to something). If you are getting values from a single column the second Rank is merely 1 to 1.
当您将工作表值分配给一个变量数组时,您总是会得到一个基于1的二维数组(例如:1到某物,1到某物;从0到某物,从0到某物)如果你从一列中得到值,第二列仅仅是1到1。
This can be proven with the following.
可以用以下方法来证明这一点。
Dim x As Long, rArray As Variant, rng As Range
Set rng = Range("D4", Range("D4").End(xlDown))
rng.NumberFormat = "0" 'don't really understand why this is here
rArray = rng.Value
Debug.Print LBound(rArray, 1) & ":" & UBound(rArray, 1)
Debug.Print LBound(rArray, 2) & ":" & UBound(rArray, 2)
For x = UBound(rArray, 1) To LBound(rArray, 1) Step -1
Debug.Print rArray(x, 1)
Next x
So you need to ask for the element in the first rank of the array; it is insufficient to just ask for the element.
所以你需要要求数组第一级的元素;仅仅要求元素是不够的。