vba二维数组初始化,在VBA中创建二维数组列表

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'

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值