VBAcomboboxoDictionary方法重复值

所以我试图通过名称和date列表循环。 用户在表单中input一个名字,然后当他们点击下拉菜单时,它将填入适用于该名称的date。 我正在尝试使用oDictionary方法来说明重复。 然而,每次我点击下拉菜单时,他们都会加倍重新填充。 所以,如果他们点击下拉菜单,然后再点击它,每个date有两个值。

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 

要使字典跨多个调用(点击)保存其内容,应将其声明为全局。

 ' outside any sub: Dim oDictionary As Object 'inside the sub: if oDictionary Is Nothing Then Set oDictionary = CreateObject("Scripting.Dictionary")