在VBA中设置数据结构

San*_*dro 7 excel vba excel-vba data-structures

我正在寻找在Excel VBA中使用的集合数据结构.到目前为止我发现的是Scripting.Dictionary,它似乎是一张地图.

在VBA中还有类似的东西吗?

基本上我正在寻找一种有效的数据结构来查明是否已经添加了特定值.

Zer*_*erk 3

您可以使用集合并执行以下功能,集合强制执行唯一的键标识符:

Public Function InCollection(Col As Collection, key As String) As Boolean
  Dim var As Variant
  Dim errNumber As Long

  InCollection = False
  Set var = Nothing

  Err.clear
  On Error Resume Next
    var = Col.Item(key)
    errNumber = CLng(Err.Number)
  On Error GoTo 0

  '5 is not in, 0 and 438 represent incollection
  If errNumber = 5 Then ' it is 5 if not in collection
    InCollection = False
  Else
    InCollection = True
  End If

End Function
Run Code Online (Sandbox Code Playgroud)