2017-03-17 161 views
0

我已创建一个节点对象:VBA - 添加自定义对象的集合在一个循环

Public value As Integer 
Public marked As Boolean 

Private Sub Class_Initialize() 
    value = 0 
    marked = False 
End Sub 

然后我尝试添加一些Node对象到集合中的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) 

这是为什么发生?

+0

你缺少一个'设置ARR =新Collection'线。 – Rory

+0

@Rory哇谢谢,那很简单.....写成答案,这样我就可以勾选它:) – Tom291

回答

2

你错过了你的循环前行:

Set arr = New Collection