![](/img/trans.png)
[英]Setting String Array as Combobox List gives Argument is required error in Visual Basic
[英]Setting an array value results in an Argument not optional error in Visual Basic
我试图在For循环中设置Collections数组的值。 但是,当我去运行程序时,它会抛出一个编译错误,指出“参数不可选”,并突出显示我设置数组值的部分。 当我调试子例程时,我无法越过ConvertbucketCollectionTobucketArray()的第一行。 此时,bucketArray元素0到12的值为Nothing,而bucketCollection包含13个元素(1-13),其中只有几个包含项。
Dim bucketCollection As New Collection 'the Collection of buckets
Dim bucketArray(12) As New Collection 'bucketCollection as an array
...
Private Sub ConvertbucketCollectionTobucketArray() 'debugger stops here
Dim newCol As Collection
Dim i As Integer
For i = 1 To bucketCollection.count
Set newCol = bucketCollection.Item(i)
bucketArray(i - 1) = newCol 'highlighted line here
Next
End Sub
由于collection是一个对象,因此在将其复制到元素时必须使用set(这是您的错误)。
Set bucketArray(i - 1) = newCol
这不会导致错误,但是不需要将数组设置为new。
Dim bucketArray(12) As Collection
并且,如果bucketCollection是一个类范围的变量,则应分别创建一个新实例并对其进行声明。 在功能之一中创建它的新实例。 否则,如果您两次运行相同的代码,则可能使用相同的实例。
Sub test()
Set bucketCollection = New Collection
populate
ConvertbucketCollectionTobucketArray
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.