我试图在名为UBidStatus
的用户窗体中使用Class属性Get
和Let
来填充名为DicOption
的字典。
一切正常,直到If Not DicOption(OptName).Exists
行(错误404对象需要)。
注意:如果我用DicOption.Add key:=OptName, Item:=OptValue
替换Public Property Let
中的整个代码,代码正在工作。
这是我正在尝试修复的Userform类中的代码。在UserForm属性中使用Dictionary.Exists会给出错误404对象所需的
'Userform Class Module
Private DicOption As scripting.Dictionary
Public Property Get ProjectOption(ByVal OptName As String) As String
ProjectOption = UBidStatus.ProjectOption(OptName)
End Property
Public Property Let ProjectOption(ByVal OptName As String, ByVal OptValue As String)
If Not DicOption(OptName).Exists Then
DicOption.Add key:=OptName, Item:=OptValue
Else
DicOption(OptName) = OptValue
End If
End Property
Public Sub UserForm_Initialize()
Set DicOption = New scripting.Dictionary
End Sub
Private Sub UserForm_Terminate()
Set DicOption = Nothing
End Sub
Public Sub ExchangeToDicOption()
Dim LR As Long
Dim Rg As Range
Dim ws As Worksheet
Dim i As Long
Dim a As String
Dim b As String
Set ws = ActiveWorkbook.Worksheets(2)
Set Rg = ws.Columns(2)
DicOption.RemoveAll
LR = Rg.Find(What:="*", Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
If LR > 1 Then
For i = 2 To LR
a = Cells(i, 1)
b = Cells(i, 2)
UBidStatus.ProjectOption(a) = b
Next i
End If
End Sub
http://stackoverflow.com/documentation/vbscript/8232/dictionary-objects#t=201702101504303204093 – Barney
我不能看到什么是我的错误指的这个文件。我尝试使用'DicOption(“OptName”)。存在'但它也没有奏效。 – Gab
请参阅“检查密钥是否存在于字典”一节下的示例 – Barney