#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
Think you should post a sample workbook.
|
#3
|
|||
|
|||
ok here is an example file. Sorry its a bit late, i tried so solve the problem by myself ... couldnt find a solution. Every entry will be copied in the combobox after clicking on the arrow for opening. I get each entry many times.
http://www.file-upload.net/download-...mple.xlsm.html |
#4
|
|||
|
|||
I'm not sure I follow what you're doing, and definitely don't follow when it's being done.
Using the _Change() event will fire the FillCombobox procedure every time any character is typed into the combo box. I'd think you would want the drop downs to populate before entering anything. Perhaps using the _GotFocus event instead of _Change. Code:
Private Sub ComboBox1_GotFocus() Call FillCombobox("List", "A", Me.ComboBox1) End Sub Code:
Sub FillCombobox(WSName As String, ColLtr As String, CBox As ComboBox) Dim LastRow As Long Set ws = ActiveWorkbook.Worksheets("List") With ws LastRow = .Cells(.Rows.Count, ColLtr).End(xlUp).Row CBox.List = .Range(ColLtr & "3:" & ColLtr & LastRow).Value End With End Sub |
#5
|
|||
|
|||
I will try it, thank you.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Using combobox to pass data from an Excel spread sheet to text boxes on userform | Stewie840 | Word VBA | 14 | 12-17-2015 10:13 PM |
combobox list gets reset....please help | Deepa Shrestha | Word VBA | 1 | 09-29-2013 09:30 PM |
VB code: populate combobox from columns in Excel file | billybeach | Outlook | 2 | 04-27-2013 04:38 AM |
Adding a long list of values to a combobox in Word 2003? | ll4u76 | Word VBA | 1 | 04-13-2012 03:37 AM |
Populating ComboBox or Drop Down list with contents of a text field | Billy_McSkintos | Word VBA | 1 | 09-13-2011 05:50 AM |