我尝试将集合作为函数输出提交给另一个过程。这是我的代码:
Function colGen() As Collection
Dim col As Collection
Dim bo As Boolean
Dim str As String
Set col = New Collection
bo = True
str = "Test"
col.Add bo
col.Add str
End Function
Sub test()
Dim col As Collection
Set col = New Collection
Dim str As String
col = colGen
MsgBox (str)
End Sub
但是在col = colGen
行中,我得到的错误是“参数不是可选的”,但只是不明白为什么会这样。有人可以帮我吗?
答案 0 :(得分:5)
你有几个问题。首先,你的函数实际上并没有返回任何东西。其次,在将对象分配给变量时需要使用<div class="col-md-6"><div class="text-align:right">some text</div></div>
:
Set
答案 1 :(得分:1)
由于一个集合是一个对象,要影响它一个新值,你必须使用Set
关键字,所以你的行应该是
Set col = colGen
而不是
col = colGen
就像你Set col = New Collection
您也忘了定义函数的输出,所以这是修改后的代码:
Function colGen() As Collection
Dim col As Collection, _
bo As Boolean, _
str As String
Set col = New Collection
bo = True
str = "Test"
col.Add bo
col.Add str
'You must assign your output value
'As it is an object, you MUST use "Set"
Set colGen = col
End Function
Sub test()
Dim col As Collection, _
str As String
Set col = New Collection
col = colGen
str = col(col.Count)
MsgBox (str)
End Sub