我有一个2列的列表,A有一个与之关联的随机数,B有一个字符串项。所以我想我可以制作阵列的任何Arraylist,但我想将数字的名称作为数字,以便可以快速引用它。我试过了
Dim row As Integer
Dim arrList As Object
Dim arr As Variant
arrList = CreateObject("System.Collections.ArrayList")
row = 1
While Not IsEmpty(ActiveSheet.Range("A" & row))
ReDim arr(0 To 1) As String
arr(0) = ActiveSheet.Range("A" & row)
arr(1) = ActiveSheet.Range("B" & row)
arrList.Add (arr)
row = row + 1
Wend
有没有快速搜索ArrList的方法(arr(0)?
答案 0 :(得分:0)
您可能应该使用Dictionary对象(https://support.microsoft.com/en-us/kb/187234)和(Does VBA have Dictionary Structure?)。
您可以使用.Exists
检查词典中是否存在您的键/项目。
Dim dctMyList As Dictionary
Dim nCount As Integer
Dim sKey As String
Set dctMyList = New Dictionary
For nCount = 1 To 5
sKey = "Fruit_" & nCount
dctMyList.Add sKey, Choose(nCount, "Apple", "Banana", "Date", "Fig", "Pear")
Next nCount
If dctMyList.Exists("Fruit_2") Then
Debug.Print dctMyList.Items(1)
Else
' Do something else
End If
请注意,您可以使用.Items
来访问内容,这是一个从零开始的索引。所以在上面的例子中,“Banana”是添加到Dictionary的第二个项目,但索引为1.