在VBA中从范围创建数组 [英] Creating an Array from a Range in VBA
问题描述
我有一个看似基本的问题,但找不到任何资源解决它。
简单地说,我只想加载一个范围的内容我可以通过
<$ p $来完成这个任务。 p>
DirArray = Array(Range(A1),Range(A2))
但是由于某些原因,我以这种方式创建数组:
DirArray = Array(Range A1:A2))
我的实际范围更长(可能会有所不同)所以我不想这样单独枚举单元格。任何人都可以告诉我如何将整个范围正确加载到数组?
后一个代码:
MsgBox UBound(DirArray,1)
And
MsgBox UBound(DirArray)
返回0,而前者返回1。
只需将变量定义为变体,他们相等:
Dim DirArray As Variant
DirArray = Range(a1:a5)。 b
不需要Array命令。
I'm having a seemingly basic problem but can't find any resources addressing it.
Simply put, I just want to load the contents of a Range of cells (all one column) into an Array.
I am able to accomplish this by means of
DirArray = Array(Range("A1"), Range("A2"))
But for some reason, I cannot create the array when expressed this way:
DirArray = Array(Range("A1:A2"))
My real Range is much longer (and may vary in length), so I don't want to have to individually enumerate the cells this way. Can anyone tell me how to properly load a whole Range into an Array?
With the latter code:
MsgBox UBound(DirArray, 1)
And
MsgBox UBound(DirArray)
Return 0, whereas with the former they return 1.
Just define the variable as a variant, and make them equal:
Dim DirArray As Variant
DirArray = Range("a1:a5").Value
No need for the Array command.
这篇关于在VBA中从范围创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!