Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在VBA中如何在数组中存储数组名_Arrays_Vba - Fatal编程技术网

Arrays 在VBA中如何在数组中存储数组名

Arrays 在VBA中如何在数组中存储数组名,arrays,vba,Arrays,Vba,我有一个数组列表。我正在循环将数组中的数据写入文本文件。每次循环时,我都希望使用不同的数组来访问数据 我正在考虑将这些数组的名称存储在不同的数组中,当我循环时,我可以使用当前循环索引访问这个数组。但我不知道如何在VBA中做到这一点 这方面需要一些指导。我也欢迎您提出其他建议。您也可以将它们存储在一个集合中。这还允许您向存储在集合中的每个数组添加一个键。您甚至可以使用此键调用特定数组。以下是一个简短的示例,让您开始学习: Sub CreateCollection() Dim col As Coll

我有一个数组列表。我正在循环将数组中的数据写入文本文件。每次循环时,我都希望使用不同的数组来访问数据

我正在考虑将这些数组的名称存储在不同的数组中,当我循环时,我可以使用当前循环索引访问这个数组。但我不知道如何在VBA中做到这一点


这方面需要一些指导。我也欢迎您提出其他建议。

您也可以将它们存储在一个集合中。这还允许您向存储在集合中的每个数组添加一个键。您甚至可以使用此键调用特定数组。以下是一个简短的示例,让您开始学习:

Sub CreateCollection()
Dim col As Collection
Dim arr As Variant
Dim MyArray1(1) As String
Dim MyArray2(1) As String

MyArray1(0) = "FirstItemArr1"
MyArray1(1) = "SecondItemArr1"
MyArray2(0) = "FirstItemArr2"
MyArray2(1) = "SecondItemArr2"
Set col = New Collection
col.Add MyArray1, "ArrayName1"
col.Add MyArray2, "ArrayName2"
For Each arr In col
    Debug.Print arr(1)
Next
Debug.Print col("ArrayName2")(1)
Set col = Nothing
End Sub

这是添加数据,你如何检索它?刚刚编辑了我的答案。给你们两个检索方法。对于每个值,循环遍历所有值总是很好的。col(“”)便于按名称(或者精确地说是键)调用数组。添加了set col=nothing以在我不再需要时销毁集合,这是一个很好的实践。显示您迄今为止的代码。我的想法不是不知道如何实现它。只需使用多维数组-
Dim数组(1到5,1到20)作为字符串
,它将有效地创建一个由5个数组组成的数组,每个数组包含20项