在数组 VBA 上为每个循环检索索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41431244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:47:36  来源:igfitidea点击:

retrieve index in a for each loop on an array VBA

vba

提问by Ku Tang Pan

I'm having a small problem with a for each loop iterating through an array of integers. I have something like this:

我在遍历整数数组的 for each 循环中遇到了一个小问题。我有这样的事情:

Dim arr(3) as Integer
Dim vari as variant

for each vari in arr
  debug.print vari
next var

While it does return the value correctly I would also need a way to refer to the index number of a given item in the array(whether it is arr(1),(2) etc). How do I do this with a for each loop? I know how to do it using a for x = y to zloop, but I'd rather keep it as a for each loop.

虽然它确实正确返回了值,但我还需要一种方法来引用数组中给定项的索引号(无论是 arr(1)、(2) 等)。我如何使用 for each 循环执行此操作?我知道如何使用for x = y to z循环来做到这一点,但我宁愿将其保留为每个循环。

回答by S Meaden

You need to use a traditional for loop if you want easy access to the element index. Try this...

如果您想轻松访问元素索引,则需要使用传统的 for 循环。尝试这个...

Sub Test2()

    Dim arr(3) As Integer
    Dim vari As Variant
    arr(0) = 5: arr(1) = 4: arr(2) = 3: arr(3) = 2

    Dim lIdx As Long
    For lIdx = LBound(arr) To UBound(arr) '* though you have defined the bounds to be 3 above !!  anyway...
        vari = arr(lIdx)
        Debug.Print vari, lIdx
    Next lIdx
'    For Each vari In arr
'      Debug.Print vari
'    Next Var
End Sub

There is noway to get the index number from a For Eachloop. If you want evidence then here is the documentation for the interface IEnumVARIANTthat underlines For Eachin VB6/VBA https://msdn.microsoft.com/en-us/library/windows/desktop/ms221053(v=vs.85).aspx

没有办法让从索引号For Each循环。如果您需要证据,那么这里是VB6/VBA https://msdn.microsoft.com/en-us/library/windows/desktop/ms221053(v=vs.85).aspxIEnumVARIANT中下划线的界面文档For Each

回答by A.S.H

If you really insist on using For Each, then you need to keep track of the index using a counter variable, i.e. the variable idxin the below code:

如果你真的坚持使用For Each,那么你需要使用计数器变量来跟踪索引,即idx下面代码中的变量:

Dim arr(1 to 3) as Integer '<-- 1 to 3 if you want your array index start with 1 instead of zero
Dim vari as variant
Dim idx as long: idx = LBound(arr)

For Each vari In arr
    debug.print "The item at index " & idx & " is: " & vari
    idx = idx + 1
Next vari

回答by Nicolas

Here try this modifying it first.

在这里尝试先修改它。

Sub forEachArray()

子 forEachArray()

Dim element As Variant
Dim animals(0 To 5) As String
'We have created an array that can hold 6 elements

animals(0) = "Dog"
animals(1) = "Cat"
animals(2) = "Bird"
animals(3) = "Buffalo"
animals(4) = "Snake"
animals(5) = "Duck-billed Platypus"
'We fill each element of the array


For Each element In animals
'animals consists of 6 "elements"

    Debug.Print element
    'printing to the immediate window

Next

End Sub

结束子