簡體   English   中英

帶數組的每個循環需要索引

[英]Need index for Each Loop with Array

我有一個數組,該數組是我用Excel表中的變量填充的。 然后,我使用一個each循環循環遍歷此數組。 數組的分配與我要填充的某些單元格對齊。

'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

如何找到每個循環的索引?

您可以通過兩種方式執行此操作。 這兩種方法都需要一種“計數器”,因為數組沒有可以訪問的任何索引屬性。

帶櫃台:

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

或在數組上使用索引循環(假定為一維數組,但可以根據需要修改為多維):

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

   ...

Next

這填充了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