VBA-向循环中的集合添加自定义对象

VBA-向循环中的集合添加自定义对象,vba,excel,object,for-loop,Vba,Excel,Object,For Loop,我已创建一个节点对象: 然后,我尝试将一些节点对象添加到for循环中的集合中: Dim inp As Integer Dim counter As Integer Dim n As node Dim arr As Collection Sub MySub() inp = InputBox("Insert a number: ") For counter = 2 To inp Set n = New node With n

我已创建一个节点对象:

然后,我尝试将一些节点对象添加到for循环中的集合中:

Dim inp As Integer
Dim counter As Integer
Dim n As node
Dim arr As Collection

Sub MySub()

    inp = InputBox("Insert a number: ")

    For counter = 2 To inp
        Set n = New node
        With n
            .value = counter
            .marked = False
        End With
        arr.Add n
    Next counter

End Sub
但当我尝试运行它时,它只会说:

Object variable or With block variable not set (Error 91)

为什么会发生这种情况?

循环前缺少一行:

Set arr = New Collection

您缺少一行
Set arr=New Collection
行。@Rory-Wow谢谢,很简单。。。。。把它写下来作为答案,这样我就可以勾选:)
Set arr = New Collection