如何在不使用键的情况下从集合中删除值? [英] How to delete a value from a collection without using it's key?

查看:71
本文介绍了如何在不使用键的情况下从集合中删除值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vba中有一个收集对象,我在其中添加了一堆ID值

I have a collection object in vba where I am adding a bunch of ID values

Dim newCollection As New Collection 
newCollection.ADD Me.ID

我是否可以从集合中删除ID,而又不知道将其存储为什么密钥?

Is there a way for me to delete an ID from the collection without knowing what key it is stored as?

我试图做:

newCollection.Remove """" & Me.ID & """" 

但是我得到了

无效的过程调用或参数

Invalid Procedure Call or Argument

推荐答案

您还需要(想要)添加键"以查找值(它必须是字符串).

You need (want) to ALSO add the "key" to lookup the value (it must be a string).

以下代码显示了其工作原理:

The following code shows how this works:

Private Sub Command103_Click()

     Dim cValues    As New Collection

     cValues.Add 5, "5"
     cValues.Add 100, "100"
     cValues.Add 6, "6"

     cValues.Add Me.ID.Value, CStr(Me.ID.Value)


     cValues.Add 200, "200"

     GoSub displayList

    ' delete the 2 value based on index
    cValues.Remove (2)
    GoSub displayList

    ' remove a value by key
    cValues.Remove (CStr(Me.ID))

    GoSub displayList

    ' remove the 6 guy by KEY
     cValues.Remove ("6")

    GoSub displayList

    Exit Sub

displayList:

     Dim i       As Integer

     For i = 1 To cValues.Count
        Debug.Print i, "--->", cValues(i)
     Next i

     Return


End Sub

输出:

1            --->           5 
2            --->           100 
3            --->           6 
4            --->           15 
5            --->           200 


1            --->           5 
2            --->           6 
3            --->           15 
4            --->           200 

1            --->           5 
2            --->           6 
3            --->           200 


1            --->           5 
2            --->           200 

这篇关于如何在不使用键的情况下从集合中删除值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆