Hey all is it at all possible to loop through a few arrays with names like:
嘿所有人都可以循环遍历一些名称如下的数组:
arryMaster = Array("170","180","120","610")
arry170 = Array("170","2214","1121"...)
arry180 = Array("180","3890","0090"...)
arry120 = Array("120","0200","7321"...)
arry610 = Array("610","1890","0213"...)
doing something like this:
做这样的事情:
For Each arryMasterLoop In arryMaster
For Each arryNumber In arryMasterLoop
if (strDeptID = arryNumber) then strGroupNum = arryNumber(0)
Next
Next
The problem I am currently having with the above code is that each of those arrays are named arryXXX and currently its looping through the arrayMasterLoop with only the number and not "arry" then the number (ex: arry170).
我目前在上面的代码中遇到的问题是每个数组都被命名为arryXXX,并且当前它只通过arrayMasterLoop循环,只有数字而不是“arry”然后是数字(例如:arry170)。
strDeptID houses one of the 2214, 2212, 3890, 0090, etc etc in the example above.
strDeptID包含上例中的2214,2212,3890,0090等之一。
How can I do this loop and let it know to look in the arryXXX variable?
如何进行循环并让它知道查看arryXXX变量?
2 个解决方案
#1
The eval function is what you're looking for to accomplish this. Here's your code tweaked to allow looping through arrays dynamically.
eval函数是您正在寻找的目标。这是你的代码调整,以允许动态循环数组。
for each arryMasterLoop in arryMaster
for each arryNumber in eval("arry" & arryMasterLoop)
' your code here
next
next
#2
For this specific problem, I might consider using an Array of Arrays. Are the array names always in the format: arryXXX ? Do some of the array names have a leading zero like array011? How many total arrays are you working with and how are they originally created? Just a few things to think on as you work through the solution.
对于这个特定问题,我可能会考虑使用数组。数组名称是否始终采用以下格式:arryXXX?有些数组名称有一个像array011这样的前导零吗?您使用了多少总数组以及它们最初是如何创建的?在您完成解决方案时,只需要考虑几件事情。
Now, what you'll want is to create an Array to hold all arrays, and insert the corresponding Array in the XXX position. Easy enough, right?
现在,您需要的是创建一个Array来保存所有数组,并将相应的Array插入XXX位置。够容易吧?
Dim maxvalue = 9999 'this is your largest array value
Dim lookupArray = Array(maxvalue);
lookupArray(170) = arry170
lookupArray(180) = arry180
lookupArray(2450) = arry2450
lookupArray(1750) = arry1750
Once you've created your lookup Array, you can easily loop through your arryMaster, parse the values as the index from which you'll retrieve the corresponding Array in lookupArray. Simple enough, right?
一旦创建了查找数组,就可以轻松遍历arryMaster,将值解析为您将在lookupArray中检索相应数组的索引。很简单吧?
Hope that helps!
希望有所帮助!
#1
The eval function is what you're looking for to accomplish this. Here's your code tweaked to allow looping through arrays dynamically.
eval函数是您正在寻找的目标。这是你的代码调整,以允许动态循环数组。
for each arryMasterLoop in arryMaster
for each arryNumber in eval("arry" & arryMasterLoop)
' your code here
next
next
#2
For this specific problem, I might consider using an Array of Arrays. Are the array names always in the format: arryXXX ? Do some of the array names have a leading zero like array011? How many total arrays are you working with and how are they originally created? Just a few things to think on as you work through the solution.
对于这个特定问题,我可能会考虑使用数组。数组名称是否始终采用以下格式:arryXXX?有些数组名称有一个像array011这样的前导零吗?您使用了多少总数组以及它们最初是如何创建的?在您完成解决方案时,只需要考虑几件事情。
Now, what you'll want is to create an Array to hold all arrays, and insert the corresponding Array in the XXX position. Easy enough, right?
现在,您需要的是创建一个Array来保存所有数组,并将相应的Array插入XXX位置。够容易吧?
Dim maxvalue = 9999 'this is your largest array value
Dim lookupArray = Array(maxvalue);
lookupArray(170) = arry170
lookupArray(180) = arry180
lookupArray(2450) = arry2450
lookupArray(1750) = arry1750
Once you've created your lookup Array, you can easily loop through your arryMaster, parse the values as the index from which you'll retrieve the corresponding Array in lookupArray. Simple enough, right?
一旦创建了查找数组,就可以轻松遍历arryMaster,将值解析为您将在lookupArray中检索相应数组的索引。很简单吧?
Hope that helps!
希望有所帮助!