【发布时间】:2018-07-09 16:57:28
【问题描述】:
我在 Excel VBA 中输入了以下代码: 该函数应该根据某个列部分中的唯一值创建一个字典。
Function CreateDictForFactors(xcord As Integer, ycord As Integer, length As Integer) As Dictionary
Dim Dict As Dictionary
Set Dict = New Dictionary
Dim i As Integer
For i = 0 To length - 1
If Not Dict.Exists(Cells(xcord + i, ycord)) Then
Dict.Add Cells(xcord + i, ycord), 0
End If
Next i
Set CreateDictForFactors = Dict
End Function
Sub test2()
Dim dict1 As Dictionary
Set dict1 = CreateDictForFactors(7, 6, 12)
End Sub
我发现此代码可作为字典和函数的示例:
Sub mySub()
dim myDict as Dictionary
set myDict = myFunc()
End Sub
Function myFunc() as Dictionary
dim myDict2 as Dictionary
set myDict2 = new Dictionary
'some code that does things and adds to myDict2'
set myFunc=myDict2
End Function
但是,当我尝试运行 makro test2 时,它会给出错误消息:
用户自定义类型未定义
谁能告诉我哪里出错了? 提前谢谢你
【问题讨论】:
标签: excel function dictionary vba