Tôi đang cố gắng sử dụng thuộc tính Lớp Get
và Let
trong UserForm được gọi là UBidStatus
để điền từ điển có tên DicOption
.
Mọi thứ hoạt động tốt cho đến khi dòng If Not DicOption(OptName).Exists
(Yêu cầu đối tượng lỗi 404).
LƯU Ý: Mã đang hoạt động nếu tôi thay thế toàn bộ mã trong số Public Property Let
theo DicOption.Add key:=OptName, Item:=OptValue
.
Đây là mã trong Lớp Userform mà tôi đang cố khắc phục.Sử dụng Dictionary.Exists trong thuộc tính UserForm đang cung cấp đối tượng Error 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
Tôi không thể nhìn thấy lỗi của tôi đề cập đến tài liệu này là gì. Tôi đã thử sử dụng 'DicOption (" OptName "). Exists' nhưng nó cũng không hoạt động. – Gab
Xem ví dụ trong phần "Kiểm tra xem khóa có tồn tại trong từ điển" – Barney