creating 2d array lists in VBA

∥☆過路亽.° 提交于 2020-03-25 16:04:44

问题


I am trying to practice with creating 2d array lists in VBA. I believe I am having a syntax issue. I am having trouble adding the y axis items into the rows. I get an error:'Invalid Procedure or argument" for multiList.Add(List).

I first initialise the x axis to create the array list rows, then I initalise the columns with combined.count.

Sub Test()

Dim xaxis As Integer
xaxis = 4


Dim combined As New ArrayList
combined.Add ("version1")
combined.Add ("version2")
combined.Add ("version3")
combined.Add ("version4")
combined.Add ("version5")
combined.Add ("version6")
combined.Add ("version7")


Dim multiList As ArrayList
Set multiList = New ArrayList

'initialise empty array list of size xaxis with lists inside
For r = 0 To xaxis


Dim List As ArrayList
Set List = New ArrayList


multiList(r).Add (List)

Next


'fill the array lists

For x = 0 To xaxis
For y = 0 To combined.Count

multiList(x).Add (combined(y))

Next y
Next x


'print the array list

For x = 0 To xaxis
For y = 0 To combined.Count

Debug.Print (multiList(x)(y))

Next y
Next x




End Sub

The overall purpose of this is to do some cross referencing and fill the 2d array list based on whether the x axis items contains or doesnt contain items from 'combined'


回答1:


Hope this is what you need:

Sub Test()
Dim xaxis As Integer
xaxis = 4

'First Arraylist Contains 7 strings
Dim combined As New ArrayList
For i = 1 To 7
    combined.Add ("version" & i)
Next i

'Second TotalArraylist contains 4 child Arraylists 1 to 4
Dim multiList As New ArrayList
Dim mul_element As ArrayList
For r = 1 To xaxis
    Set mul_element = New ArrayList
    multiList.Add mul_element
Next

'fill the TotalArraylist lists

For Each mul_element In multiList '[4 child Arraylists]
    For y = 0 To combined.Count - 1   '[7 strings] 0 to 6
        mul_element.Add (combined(y))
    Next y
Next mul_element

'print the string stores in mul_element(Child arraylist) in multiList(Total arraylist)

For x = 0 To multiList.Count - 1 '[4 element] 0 to 3
    For y = 0 To combined.Count - 1  '[7 element] 0 to 6
        Debug.Print multiList(x)(y)
    Next y
Next x
End Sub


来源:https://stackoverflow.com/questions/60662952/creating-2d-array-lists-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!