0

考虑下面的 ArrayList:

   Dim myAL As New ArrayList()
    myAL.Add("The")
    myAL.Add("quick")
    myAL.Add("brown")
    myAL.Add("fox")
    myAL.Add("jumped")
    myAL.Add("over")
    myAL.Add("the")
    myAL.Add("lazy")
    myAL.Add("dog")

那么现在我们可以将其值设置为水平的 Excel 范围吗?还是在此之前我们需要执行任何转换?

代码

Option Explicit

    Dim myQueue,i
    Set myQueue = CreateObject("System.Collections.ArrayList")
    'Dim Set Dic = CreateObject("Scripting.Dictionary

    myQueue.Add("jumped")
    myQueue.Add("over")
    myQueue.Add("")
    myQueue.Add("")
    myQueue.Add("dog")
    myQueue.Add("Cat")

    i=0
    Do While myQueue.Count > i

      MsgBox(myQueue(i))
    i=i+1
    Loop

    MsgBox("Capacity=" & myQueue.Capacity & "Count:" & myQueue.Count)

    myQueue.Remove("dog")

    MsgBox("Capacity=" & myQueue.Capacity & "Count:" & myQueue.Count)

    i=0
    Do While myQueue.Count > i

      MsgBox(myQueue(i))
    i=i+1
    Loop

当我们执行任何删除操作时,ArrayList 的大小会像字典对象一样自动调整,还是我们需要使用任何(Re)Dim语句来处理大小?

谢谢,

4

1 回答 1

1

首先,您需要将 ArrayList 设置为数组:

listArray = myAl.ToArray()

然后水平粘贴:

Range("A1").Resize(, UBound(listArray, 1) + 1) = listArray

垂直粘贴:

Range("A1").Resize(UBound(listArray, 1) + 1) = Application.Transpose(listArray)

当您使用 Remove / RemoveRange 删除项目时,您可以调用该.Count属性来获取数组的更新大小。数组列表上没有 Ubound/LBound。

于 2012-12-23T10:32:02.077 回答