VBA - 查询自定义类的集合

时间:2017-08-23 14:24:28

标签: excel vba excel-vba

VBA中是否有办法查询名为People的自定义类的集合。假设我有一个具有名字,姓氏和标题的自定义类。

‘private attributes
Private pFirstName as String
Private pLastName as String
Private pTitle as String

‘Get/Let Methods
Public Property Get FirstName() as String
FirstName = pFirstName
End Property

Public Property Let FirstName (Value as String)
pFirstName = Value
End Property

Public Property Get LastName() as String
LastName = pLastName
End Property

Public Property Let LastName(Value as String)
pLastName = Value
End Property

Public Property Get Title() as String
Title = pTitle
End Property

Public Property Title Let (Value as String)
pTitle = Value
End Property

然后,在我的主要SUB中,创建一个人的集合。有没有办法查询该集合,即,给我所有名字==杰克的人。

由于

1 个答案:

答案 0 :(得分:3)

你可以不遗余力地实施something like this,这样你就可以做一些疯狂的事情:

Dim items As LinqEnumerable
Set items = LinqEnumerable.FromCollection(myCollection) _
                          .Where("x => x.FirstName = ""Jack""")

Dim p As Person '"People" is plural, you don't want a pluralized class name here.
For Each p In items
    Debug.Print p.FirstName
Next

但这非常过分,效率低下。您所需要的只是一个循环和一个条件:

For Each p In myCollection
    If p.FirstName = "Jack" Then
        'we have a winner
    End If
Next