Thread: [Solved] Excel Combobox List
View Single Post
 
Old 12-21-2015, 04:46 AM
VBA-User VBA-User is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Dec 2015
Posts: 5
VBA-User is on a distinguished road
Default Excel Combobox List

Hello,

I try to fill a combobox with Text from an excelsheet. The code i wrote works fine, but when i select my combobox a second time, then the entries will appear a second time in my combobox and after clicking a third time on the arrow to open it, all entries are three times there and so on. I am not using an userform, i have inserted the an activeX element for combobox in the excel sheet from Developer tools. Here is the code:

Private Sub ComboBox2_Change()

Dim WS As Worksheet
Dim LastRow As Long
Dim aCell As Range
Dim lIndxA As Long
Dim lIndxI As Long
Dim sTemp As String

ComboBox2.Clear

Call FillCombobox("List", "B", Me.ComboBox2)

Set WS = ActiveWorkbook.Worksheets("List")

With WS
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

For Each aCell In .Range("B2:B500" & LastRow)
If aCell.Value <> "" Then
Me.ComboBox2.AddItem aCell.Value
End If
Next

For lIndxA = 0 To Me.ComboBox2.ListCount - 1
For lIndxI = 0 To lIndxA - 1
If Me.ComboBox2.List(lIndxI) > Me.ComboBox2.List(lIndxA) Then
sTemp = Me.ComboBox2.List(lIndxI)
Me.ComboBox2.List(lIndxI) = Me.ComboBox2.List(lIndxA)
Me.ComboBox2.List(lIndxA) = sTemp
End If
Next lIndxI
Next lIndxA

End With

End Sub

I hope someone can help me.

regards
Reply With Quote