我有一个函数
Function convertToDict(arrayIp As Variant) As Object
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")
For Each element In arrayIp
If dict.exists(element) Then
dict.Item(element) = dict.Item(element) + 1
Else
dict.Add element, 1
End If
Next
End Function
我尝试从子进程调用此函数
Dim dict As Object
varray = Range("B4:B" & finalRow - 1).Value
dict = convertToDict(varray)
但它抛出错误:
Run time error 450, wrong number of arguments or invalid property
我在这里犯了什么错误?
我正在传递一个变体,结果是一个对象
。
最佳答案
因为您正在处理对象,所以您需要在函数和子函数中Set:
Function convertToDict(arrayIp As Variant) As Object
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")
For Each element In arrayIp
If dict.exists(element) Then
dict.Item(element) = dict.Item(element) + 1
Else
dict.Add element, 1
End If
Next
Set convertToDict = dict
End Function
Sub qwerty()
Dim dict As Object
finalRow = 10
varray = Range("B4:B" & finalRow - 1).Value
Set dict = convertToDict(varray)
End Sub
关于vba - 将变体作为参数传递给 VBA 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901783/