【发布时间】:2014-05-28 01:47:31
【问题描述】:
希望这是一个简单的,但我似乎找不到任何参考。
我如何打印存储在数组中的东西的位置,而不是其中的实际项目。
Array(0) = Dog
Array(1) = Cat
Array(2) = Fish
假设我搜索了数组并找到了猫,我如何打印存储猫的位置,在本例中为索引号 (1)。
提前致谢。
【问题讨论】:
标签: arrays vbscript indexing location
希望这是一个简单的,但我似乎找不到任何参考。
我如何打印存储在数组中的东西的位置,而不是其中的实际项目。
Array(0) = Dog
Array(1) = Cat
Array(2) = Fish
假设我搜索了数组并找到了猫,我如何打印存储猫的位置,在本例中为索引号 (1)。
提前致谢。
【问题讨论】:
标签: arrays vbscript indexing location
数组的位置被称为index
如果你运行一个循环,
For i = LBound(Array) to UBound(Array)
if Array(i) = "Cat" then '--restrict to find index of particular item
MsgBox i '-- gives the location/index of Cat item
End if
next i
LBound :是下限,数组的起始索引。首先。它可以是零或任何值,因为 VBA 提供了将默认数组基数更改为 0 或 1 的灵活性。
UBound :是上界,数组的结束索引。最后一个。
进一步阅读:LBound and Ubound conflicts in case of array which has been assigned by the Range.
【讨论】:
Array(i) 返回,并且索引返回为 i ;)
对于这种情况,最好使用ArrayList,而不是简单的数组。
Set myArray = CreateObject ("System.Collections.ArrayList")
With myArray
.Add "Dog"
.Add "Cat"
.Add "Fish"
End With
intIndex = myArray.IndexOf ("Cat",0)
此外,您不必关心边界。
【讨论】: