我想将以下项目添加到组合框中,但如果有重复的项目,则只应添加一个。
A
1 john
2 john
3 marry
4 marry
5 john
6 lisa
7 frank
8 marry
我希望组合框结果是john
, marry
, lisa
and 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
object.
见下文:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)