将数组添加到 VBA 中的集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17933983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:28:25  来源:igfitidea点击:

Add array to Collection in VBA

arraysvbacollections

提问by hartley054

So I basically have a list of rows in excel containing data from a calendar, all with each user who created them. I am making a collection of objects with each users. After adding the individual to the 1st collection I then have another collection called "Events" inside the user object. This is where the event type and dates are stored. I have having trouble adding an array to the Collection "Events".

所以我基本上在 excel 中有一个包含日历数据的行列表,所有这些都与创建它们的每个用户有关。我正在为每个用户制作一个对象集合。将个人添加到第一个集合后,我在用户对象中有另一个名为“事件”的集合。这是存储事件类型和日期的地方。我在向集合“事件”中添加数组时遇到问题。

The "user" object looks like this:

“用户”对象如下所示:

User

-Name

-Events [event1, event2, event3, ..., etc]

用户

-姓名

-事件 [event1, event2, event3, ..., etc]

I'm getting the error of "Object variable or With variable not set" Here are the lines taken from the code below at the end of the neested if and else if statements that give this error:

我收到了“对象变量或未设置变量”的错误信息以下是在给出此错误的 if 和 else if 语句末尾从下面的代码中截取的行:

temp.Events.Add arr1

emp2.Events.Add arr2

temp.Events.Add arr1

emp2.​​Events.Add arr2

Code:

代码:

Do Until IsEmpty(Cells(i, 5))
Dim name As String
'grab name in cell
name = Cells(i, 5)
'if first list item, add new user
If list.Count = 0 Then
    Dim emp1 As New User
    emp1.name = name
    list.Add emp1
Else
    'traverse through list and search for same name
    For j = 1 To list.Count
        'if name is present, add event data to user object
        If list.Item(j).name = name Then
            Dim temp As New User
            Set temp = list.Item(j)
            Dim arr1(2) As String
            arr1(1) = Cells(i, 3)
            arr1(2) = Cells(i, 1)
            temp.Events.Add arr1
        'if name is not present, add new user and event data to new user object
        ElseIf j = list.Count Then
            Dim emp2 As New User
            emp2.name = name
            Dim arr2(2) As String
            arr2(1) = Cells(i, 3)
            arr2(2) = Cells(i, 1)
            emp2.Events.Add arr2
            list.Add emp2
        End If
    Next
End If

    i = i + 1
Loop

If there is any easier way of doing this or if this isn't possible please point me in the right direction. Any help is greatly appreciated. Thanks.

如果有任何更简单的方法可以做到这一点,或者如果这是不可能的,请指出我正确的方向。任何帮助是极大的赞赏。谢谢。

采纳答案by Sorceri

In your User class add the method Class_Initialize and initialize your variables. Now this only gets called on the first method used so when you use

在您的 User 类中添加方法 Class_Initialize 并初始化您的变量。现在这只会在使用的第一个方法上被调用,所以当你使用

Dim temp As New User 

it is still not initialized until you call a method of the object.

在您调用对象的方法之前,它仍未初始化。

You will want to use the below way to initialize it before use

您将需要使用以下方式在使用前对其进行初始化

Dim temp As User
set temp = New User 'now it is initialized

In your User Class

在您的用户类中

Private Sub Class_Initialize()
'code here 
End Sub