我想将以下项目添加到组合框,但是如果某项目重复,则仅应添加一个。
A
1 john
2 john
3 marry
4 marry
5 john
6 lisa
7 frank
8 marry
我想组合框的结果是john
,marry
,lisa
和frank
(而不是八个项目四个独特的项目)。
我的代码是:
Private Sub Workbook_Open()
Application.EnableEvents = False
With Sheet2.ComboBox1
For Each Cell In Sheet1.Range("A1:A6348")
If Not ComboBox1.exists(Cell.Value) Then
.AddItem Cell.Value
End If
Next
End With
End Sub
添加唯一项的另一种方法是使用Dictionary
对象。
见下文:
Dim rngItems As Range
Dim oDictionary As Object
Set rngItems = Range("A1:A8")
Set oDictionary = CreateObject("Scripting.Dictionary")
With Sheet1.ComboBox21
For Each cel In rngItems
If oDictionary.exists(cel.Value) Then
'Do Nothing
Else
oDictionary.Add cel.Value, 0
.AddItem cel.Value
End If
Next cel
End With
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句