我正在嘗試在子中使用的函式中創建字典,但出現錯誤
“未定義用戶定義型別”。
我不知道這是否很明顯,但我看不出有什么問題。
我已經簡化了代碼以制作一個簡單的代碼:
Public Function create_dic(ByVal var As String) As Dictionary
my_dic.CompareMode = vbTextCompare
If var = "test" Then
my_dic.Add Key:="var 1", Item:=1
Else
my_dic.Add Key:="var 2", Item:=2
End If
Set create_dic = my_dic
End Function
Sub prueba()
Set respuesta = create_dic("test")
End Sub
uj5u.com熱心網友回復:
您是否設定了對“Microsoft Scripting Runtime”的參考?Dictionary不是標準 VBA 的一部分,需要該庫。請參閱VBA 字典參考。
此外,當您使用my_dic.CompareMode它時,它還不存在。確保正確使用Option Explicit和宣告所有變數。此外,您需要先創建一個新的字典物件,然后才能使用它。
Option Explicit
Public Function create_dic(ByVal var As String) As Dictionary
Dim my_dic As Dictionary ' declare variable type
Set my_dic = New Dictionary ' create a new dictionary object
my_dic.CompareMode = vbTextCompare
If var = "test" Then
my_dic.Add Key:="var 1", Item:=1
Else
my_dic.Add Key:="var 2", Item:=2
End If
Set create_dic = my_dic
End Function
Public Sub prueba()
Dim respuesta As Dictionary ' declare variable type
Set respuesta = create_dic("test")
End Sub
有關字典的更多資訊,這是一個很好的參考:Excel VBA 字典 - 完整指南。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/464563.html
上一篇:來自UI的閃亮觸發服務器功能
下一篇:當所有值都是NA時求和/回傳NA
