简体   繁体   English

带数组的每个循环需要索引

[英]Need index for Each Loop with Array

I have an array which I populate with Varibles from an excel table. 我有一个数组,该数组是我用Excel表中的变量填充的。 I then use an each loop to cycle through this array. 然后,我使用一个each循环循环遍历此数组。 The allocation of the array lines up with some cell I would like to populate. 数组的分配与我要填充的某些单元格对齐。

'arRow is a Dynamic Array, that varies in size
For each vIndex in arRow
 if vIndex = 0 then
     'do nothing
   else
    'Populate corisponding cell
     Cells(2, ???).value = vIndex
  end if
next vindex

How would I find the index for the Each-Loop? 如何找到每个循环的索引?

You can do this two ways. 您可以通过两种方式执行此操作。 Both methods require a "counter" of sorts, as the array doesn't have any sort of indexed property you can access. 这两种方法都需要一种“计数器”,因为数组没有可以访问的任何索引属性。

With a counter: 带柜台:

Dim i as Long
i = 0
For each vIndex in arRow
 i = i + 1
 if vIndex = 0 then
     'do nothing
   else
    'Populate corisponding cell
     Cells(2, i).value = vIndex
  end if
next vindex

Or with an indexed loop over the array (assumes a 1-dimensional array, but could be modified for multi-dimensional if needed): 或在数组上使用索引循环(假定为一维数组,但可以根据需要修改为多维):

Dim i
For i = LBound(arRow) to UBound(arRow)

   ...

Next

This fills B1:B3 这填充了B1:B3

arRow = Array(11, 22, 33)

For vIndex = 0 To UBound(arRow)
    Cells(vIndex + 1, 2).Value = arRow(vIndex)
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM