如何在vba中创建循环以块的形式迭代列表?

MIT*_*THU 2 vba list

我试图从主列表创建 3 个子列表itemlist,但我找不到任何方法来实现这一点。当我查看此链接时,我在 python 中找到了解决方案。

Sub CreateSubList()
    Dim itemlist As Variant, itemNum As Variant
    Dim oSublist As Variant
    
    itemlist = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
    
    'here I was expecting to create a variable holding sublist, as in oSublist which contains Array("1", "2", "3", "4")
    'Array("5", "6", "7", "8") and Array("9", "10", "11") in each loop
    'and finally iterate over the list in chunk to print the result
    
    For Each itemNum In oSublist
        Debug.Print itemNum
    Next itemNum
End Sub
Run Code Online (Sandbox Code Playgroud)

具体来说,我试图在 vba 中模仿这一点:

itemlist = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]

for i in range(0, len(itemlist), 4):
    chunk = itemlist[i:i + 4]
    
    for n in chunk:
        print(n)
Run Code Online (Sandbox Code Playgroud)

看来我已经非常接近解决这个问题了。

Sub CreateSubList()
    Dim itemlist As Variant, itemNum As Variant
    Dim oSublist As Variant, iCol As Collection
    Dim inum As Variant
    
    Set iCol = New Collection

    itemlist = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")

    For Each inum In itemlist
        iCol.Add inum
        
        If iCol.Count >= 3 Then
            For Each itemNum In iCol
                Debug.Print itemNum
            Next itemNum
            
            Debug.Print "---------------------"
            Set iCol = Nothing
            Set iCol = New Collection
        End If
    Next inum
End Sub
Run Code Online (Sandbox Code Playgroud)

我得到的输出:

1
2
3
---------------------
4
5
6
---------------------
7
8
9
---------------------
Run Code Online (Sandbox Code Playgroud)

然而,仍未解决的是我无法捕获这两个项目,如在1011有条件地从itemlist.

Але*_*й Р 5

选项 1 - 使用锯齿状数组

CreateSubLists()Array(Array(), Array(), ...)从原始数组的切片元素返回一个锯齿状数组。

Option Explicit

Function CreateSubLists(itemlist, count)
    Dim amount, chunks, i, j, retval, a
    amount = UBound(itemlist) - LBound(itemlist) + 1        'get the amount of the elements
    chunks = WorksheetFunction.RoundUp(amount / count, 0)   'calculate the number of chunks (e.g. if 2.5 then 3)
    
    ReDim retval(0 To chunks - 1)   'make the outer array
    For i = 0 To UBound(retval)
        'make the inner array. If the rest of the itemlist is less then chunk then get the size of the rest
        ReDim a(0 To WorksheetFunction.Min(count - 1, amount - i * count - 1))
        For j = 0 To UBound(a)
            a(j) = itemlist(i * count + j)  'fill the inner array
        Next
        retval(i) = a   'place the inner array into the outer array
    Next
    CreateSubLists = retval 'return the jagged array
End Function

Sub Example()
    Dim itemlist, chunk
    itemlist = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
    For Each chunk In CreateSubLists(itemlist, 4)  ' works with any positive integer number
        Debug.Print "[" & Join(chunk, ",") & "]"
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

选项 2 - 使用集合

Function CreateSubLists2(itemlist, count) As Collection
    Dim amount, chunks, i, j, a
    amount = UBound(itemlist) - LBound(itemlist) + 1        'get the amount of the elements
    chunks = WorksheetFunction.RoundUp(amount / count, 0)   'calculate the number of chunks (e.g. if 2.5 then 3)
    
    Dim retval As New Collection   'make the outer collection
    For i = 0 To chunks - 1
        'make the inner array. If the rest of the itemlist is less then chunk then get the size of the rest
        Set a = New Collection
        For j = 0 To WorksheetFunction.Min(count - 1, amount - i * count - 1)
            a.Add itemlist(i * count + j)  'fill the inner collection
        Next
        retval.Add a   'place the inner collection into the outer collection
    Next
    Set CreateSubLists2 = retval 'return the collection of collections
End Function

Sub Example2()
    Dim itemlist, chunk, el, s
    itemlist = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
    For Each chunk In CreateSubLists2(itemlist, 4)  ' works with any positive integer number
        s = "["
        For Each el In chunk
            s = s & el & ","
        Next
        s = s & "]"
        Debug.Print Replace(s, ",]", "]")
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

印刷:

[1,2,3,4]
[5,6,7,8]
[9,10,11]
Run Code Online (Sandbox Code Playgroud)