如何使用其他数组中的坐标获取数组值? [英] How to get array value using coordinates in other array?

查看:122
本文介绍了如何使用其他数组中的坐标获取数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用类似数组中的其他坐标来访问数组。对于这种情况,我无法事先知道数据数组中的维数,因此不能真正在函数中使用不确定数量的可选变量。

I'm looking to access an array using coordinates from a different array, like such. This for a situation where I don't on forehand know the number of dimensions in the data array, so can't really just use an undetermined number of optional variables in a function.

Dim myArray(1 To 4, 1 To 2) As String
Dim myCoord(1 To 2) As Long

myArray(1, 1) = "one_one"
myArray(1, 2) = "one_two"
...
myArray(4, 2) = "four_two"

myCoord(1) = 3
myCoord(2) = 1

MsgBox(myArray(myCoord))

所以我正在寻找类似上面的消息框的东西,它能够显示 three_one。就像在python的 my_multidim_list [* [i,j,...,n]] 一样,在VBA中完全不可能,但是好吧,我可以实现这种可能性。

So I'm looking for something like the above messagebox being able to display "three_one". Like in python's my_multidim_list[*[i, j, ..., n]] No idea if it's at all possible in VBA, but well, doesn't seem illogical to me to implement such a possibility.

推荐答案

这是我最初的回答,它为VBA阵列提供了一些背景知识。我将对其进行扩展,以提供足够的背景来理解我的第二个答案。

This was my original answer which provides some background on VBA arrays. I will be expanding it to provide enough background to understand my second answer.

简单的答案是:

Dim myArray(1 To 4, 1 To 2) As String
Dim myCoord(1 To 2) As Long

myArray(1, 1) = "one_one"
myArray(1, 2) = "one_two"
...
myArray(4, 2) = "four_two"

myCoord(1) = 3
myCoord(2) = 1

MsgBox(myArray(myCoord(1), myCoord(2)))   ' This is the only change

这基于 myCoord 的每个元素,定义了相应维度的元素编号 myArray

This is based on each element of myCoord defining the element number of the corresponding dimension of myArray.

有关数组的其他信息

当编写 Dim myArray(1 to 4,1 To 2)as String 时,维数和每个维中的元素数是固定的直到用不同的数字重写此语句。

When you write Dim myArray(1 To 4, 1 To 2) As String, the number of dimensions and the number of elements in each dimension are fixed until you rewrite this statement with different numbers.

如果您将 Dim myArray()As String 写入,则表示ng数组,但是维数及其边界的数目将在运行时定义。

If you write Dim myArray() As String, you are declaring the array but the number of dimensions and their bounds will be defined at run time.

在您的代码中,您可以编写 ReDim myArray(a To b,c To d,e To f)其中a至f是整数表达式。在我所知道的大多数语言中,下限由该语言定义为0或1。对于VBA,只要下限不超过上限,则下限可以是任何值。我只有一次发现负下限的用途,但是有选项。

Within your code you can write ReDim myArray(a To b, c To d, e To f) where a to f are integer expressions. In most languages I know, the lower bound is defined by the language as 0 or perhaps 1. With VBA, the lower bound can be anything providing the lower bound is not more than the upper bound. I have only once found a use for a negative lower bound but the option is there.

稍后您可以编写 ReDim myArray(g To h) ,但是您将丢失myArray中的所有数据。

Later you can write ReDim myArray(g To h) but you will lose all the data within myArray.

或者,您可以编写 ReDim Preserve myArray(a to b ,c To d,e To g)。请注意,a到e不变。使用 ReDim Preserve 只能更改最后一个尺寸的上限。 ReDim Preserve 创建一个新的更大(或更小)的数组,从旧数组中复制数据,并将新元素初始化为数据类型的默认值。过度使用 ReDim Preserve 可能会使您的宏慢到爬网的速度,因为解释器的内存不足,但如果使用得当,它会非常有用。

Alternatively, you can write ReDim Preserve myArray(a To b, c To d, e To g). Note that a to e are unchanged. With ReDim Preserve only the upper bound of the last dimension can be changed. ReDim Preserve creates a new larger (or smaller) array, copies data from the old array and initialises the new elements to the default value for the data type. Over use of ReDim Preserve can slow your macro down to a crawl because the interpreter runs out of memory but if used carefully it can be very useful.

我可能会定义 myCoords ,其维数与 myArray 相同,但这取决于

I would probably define myCoords with the same number of dimensions as myArray but that depends on your objective.

关于VBA阵列,我还有很多话要说。如果您扩展自己的目标,我会添加适当的额外信息。

There is a lot more I could say about VBA arrays. If you expand on your objectives I will add appropriate extra information.

这篇关于如何使用其他数组中的坐标获取数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆