所以我试图遍历一个名单和日期列表。用户在表单上键入名称,然后在单击下拉列表时,应填充适用于该名称的日期。我试图使用oDictionary方法来解释重复。然而,每次我点击下拉列表时,他们都会重复填充。因此,如果他们单击下拉列表然后重新单击它,则每个日期都有两个值。
Private Sub cbCancelApptDate_DropButtonClick()
Dim NamePop As String
Dim NameRange As Range
Dim cell As Range
Dim Countcells As Integer
NamePop = tbNewApptName.Value
'Search All Appointments
If Sheets("All Appointments").Range("C3") = "" Then
Set NameRange = Sheets("All Appointments").Range("C2:C2")
Else
Set NameRange = Sheets("All Appointments").Range("C2", Sheets("All Appointments").Range("C2").End(xlDown))
End If
Dim oDictionary As Object
Set oDictionary = CreateObject("Scripting.Dictionary")
With Me.cbCancelApptDate
For Each cell In NameRange
If NamePop = cell.Value Then
If oDictionary.exists(cell.Offset(, -2).Value) Then
'Do Nothing
Else
oDictionary.Add cell.Offset(, -2).Value, 0
.AddItem cell.Offset(, -2).Value
End If
End If
Next cell
End With
答案 0 :(得分:1)
要使字典在多个调用(点击)中保存其内容,应将其声明为全局。
' outside any sub:
Dim oDictionary As Object
'inside the sub:
if oDictionary Is Nothing Then Set oDictionary = CreateObject("Scripting.Dictionary")