反转集合中的项目

发布于 2024-10-03 02:29:33 字数 30 浏览 3 评论 0原文

反转 VBA 集合中的项目的最简单方法是什么?

What is the easiest way to reverse the items in a VBA collection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喵星人汪星人 2024-10-10 02:29:33

不知道有什么巧妙的方法,但你可以这样做(代码未测试):

Dim MyNewCol as New Collection
For Each obj in MyCol
    If MyNewCol.Count > 0 Then
        MyNewCol.Add item := obj, before := 1
    Else
        MyNewCol.Add item := obj
    End If
Next

Don't know of any neat way of doing it but you could do something like (code not tested):

Dim MyNewCol as New Collection
For Each obj in MyCol
    If MyNewCol.Count > 0 Then
        MyNewCol.Add item := obj, before := 1
    Else
        MyNewCol.Add item := obj
    End If
Next
泪眸﹌ 2024-10-10 02:29:33

从具有最大索引的成员开始逐步向下遍历起始集合。将每个成员添加到反向集合中。

Sub ReverseCollection(aCollection)

    Dim ndx As Integer
    Dim max As Integer
    Dim reversedCollection As New Collection
    max = aCollection.Count + 1

    For ndx = 1 To aCollection.Count
        reversedCollection.Add aCollection(max - ndx)
    Next ndx
End Sub

Steps down through the starting collection beginning from the member with the largest index. Adds each member to the reversed collection.

Sub ReverseCollection(aCollection)

    Dim ndx As Integer
    Dim max As Integer
    Dim reversedCollection As New Collection
    max = aCollection.Count + 1

    For ndx = 1 To aCollection.Count
        reversedCollection.Add aCollection(max - ndx)
    Next ndx
End Sub
肥爪爪 2024-10-10 02:29:33

在起始集合上使用内置迭代器。
要使用“添加,之前”,集合必须至少有 1 个成员,因此添加一个项目,然后在反向集合完成后将其删除。

Sub ReverseCollection2(aCollection)

   Dim item As Variant
   Dim reversedCollection As New Collection
   reversedCollection.Add "dummy entry"
   For Each item In aCollection
       reversedCollection.Add item, Before:=1
   Next item
   reversedCollection.Remove reversedCollection.Count
End Sub

Uses the built-in iterator over the starting collection.
To use 'Add, Before', the collection must have at least 1 member, so add an item and then remove it after the reversed collection is finished.

Sub ReverseCollection2(aCollection)

   Dim item As Variant
   Dim reversedCollection As New Collection
   reversedCollection.Add "dummy entry"
   For Each item In aCollection
       reversedCollection.Add item, Before:=1
   Next item
   reversedCollection.Remove reversedCollection.Count
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文