VBA结构的通用列表
我开始在Excel中使用VBA工作,并希望保存某种List。
我的方法如下,但是我担心我正在研究完全错误的方向。
Public Type MyType ID As Integer Name As String MyInt As Integer End Type Public ListVar As New Collection Public Sub MySub Dim MyVar As MyType MyVar.ID = 1 MyVar.Name = "name" MyVar.MyInt = 2000 ListVar.Add MyVar, MyVar.ID End Sub
当试图执行时,我得到一个编译错误:“只有在公共对象模块中定义的用户定义的types可以强制到或来自变体,或传递到后期绑定的函数。
目标是有一个C#喜欢列表,我可以添加MyType对象,并find它们,你可以在C#中完成:ListVar.find(p => p.ID == 3)。 另外,如果可行,列表应该在文件中保留。 这是不需要的,因为我可以在closures时将其写入工作表中,反之亦然。
我可以使用的另一个对象类是否可以更好地满足我的需求?
你可以使用数组列表? 在你的Sub:
Dim arrayList As Object Set arrayList = CreateObject("System.Collections.ArrayList")
然后,您可以使用.NETtypesArrayList的所有常用方法,如.Contains()
或.Sort()
等…
编辑:( 如OP是使用Mac OS)
你可以自己创build一个类,并在集合中使用它。 首先创build一个新的类模块( Alt + I ,然后C )
在这个模块中使用以下内容:
Private sID As Integer Private sName As String Private sInt As Integer Public Property Let ID(value As Integer) sID = value End Property Public Property Get ID() As Integer ID = sID End Property Public Property Let Name(value As String) sName = value End Property Public Property Get Name() As String Name = sName End Property Public Property Let MyInt(value As Integer) sInt = value End Property Public Property Get MyInt() As Integer MyInt = sInt End Property
重要 – 将模块命名为“MyType”,这是您要在代码中使用的名称。
返回到正常代码模块中的原始子代码:
Public ListVar As New Collection Public Sub MySub() Dim MyVar As New MyType MyVar.ID = 1 MyVar.Name = "name" MyVar.MyInt = 2000 ListVar.Add MyVar, CStr(MyVar.ID) End Sub
将工作正常。
不知道你是否可以在Mac上做到这一点,但我使用类似这样的类,
Option Explicit Implements Scripting.Dictionary Private dic As Scripting.Dictionary Private Sub Class_Initialize() Set dic = New Scripting.Dictionary End Sub Public Sub Add(Key As Variant, Item As Variant) dictionary_Add Key, Item End Sub Private Sub dictionary_Add(Key As Variant, Item As Variant) dic.Add Key, Item End Sub Public Sub Remove(Key As Variant) dictionary_Remove (Key) End Sub Private Sub dictionary_Remove(Key As Variant) dic.Remove (Key) End Sub Public Function TEST_LAMBDA(strLAMBDA As String) As Scripting.Dictionary Dim strSplit() As String Dim strCriteria As String Set TEST_LAMBDA = New Scripting.Dictionary strSplit = Split(strLAMBDA, "=>") strCriteria = strSplit(1) Dim intCounter As Integer strSplit = Split(strCriteria, "=") strCriteria = strSplit(1) For intCounter = 0 To dic.Count - 1 If dic.Keys()(intCounter) = strCriteria Then TEST_LAMBDA.Add dic.Keys()(intCounter), dic.Items()(intCounter) End If Next intCounter End Function Private Property Get dictionary_CompareMode() As Scripting.CompareMethod End Property Private Property Let dictionary_CompareMode(ByVal RHS As Scripting.CompareMethod) End Property Private Property Get dictionary_Count() As Long End Property Private Function dictionary_Exists(Key As Variant) As Boolean End Function Private Property Get dictionary_HashVal(Key As Variant) As Variant End Property Private Property Get dictionary_Item(Key As Variant) As Variant End Property Private Property Let dictionary_Item(Key As Variant, RHS As Variant) End Property Private Property Set dictionary_Item(Key As Variant, RHS As Variant) End Property Private Function dictionary_Items() As Variant End Function Private Property Let dictionary_Key(Key As Variant, RHS As Variant) End Property Private Function dictionary_Keys() As Variant End Function Private Sub dictionary_RemoveAll() End Sub
我是这样用的
Sub t() Dim a As New clsDictionary a.Add "9", "this out" a.Add "10", "test" Dim d As Scripting.Dictionary Set d = a.TEST_LAMBDA("x=>x=9") End Sub