简体   繁体   中英

Sometimes the ActiveX Combobox only shows one row, why?

It seems that when I first click on the combobox and then click on the arrow, all items are displayed.

While if I click on the arrow without clicking on the combobox before, only one item is displayed and I can click on scroll buttons to see the other items.

Why does this happen?

Here is the macro I am using to populate the combobox with items

Private Sub ComboBox1_GotFocus()
    Dim c As Range
    Dim selText As String
    selText = ComboBox1.selText
    ComboBox1.Clear
    For Each c In wConfig.Range("BudgetDropdown").Cells
        ComboBox1.AddItem c.Value
    Next c
    ComboBox1.selText = selText
End Sub

在此处输入图片说明

在此处输入图片说明

To automatically populate combobox with data from named range, set it's ListFillRange property to the name of the range.

You can do it at runtime:

ComboBox1.ListFillRange = "BudgetDropdown"

Or by setting it to BudgetDropdown in properties window.

Don't know exactly why, but basically when you open a just-cleared-combobox it shows up only 1 lane because it "thinks" it is empty even if you have just refilled it. To trick it, you have to remove all the rows 1 by 1 till you reach your ListCount value (the number of rows your combobox will display). Then you can add all the new rows and after that you can delete the previously omitted rows. In my case i could not use the ListFillRange because my list was "embended" in several cells; so i had to extract it. Something like this:

Private Sub Combobox3_GotFocus()

Dim RngRange01 As Range 'Single cell where my row is "embended"
Dim IntCounter01 As Integer 'A counter
Dim BlnCheck as Boolean 'A boolean to remember me if it's the Combobox was empty before i
'focused it

IntCounter01 = ComboBox3.ListCount 'Set the counter equal to ListCount

'I check if the combobox is already filled or not and modify the BlnCheck
BlnCheck = True
If ComboBox3.ListCount = 0 Then BlnCheck = False

'In this For-Next i remove all the rows till i reach the ListRows
For IntCounter01 = IntCounter01 To ComboBox3.ListRows + 1 Step -1
    ComboBox3.RemoveItem IntCounter01 - 1
Next IntCounter01

'Set the range (it's a named list with titles located in another sheet)
Set RngRange01 = Sheets("MySheet1").Cells(2, Sheets("MySheet1").Range("MyList").Column)


Do Until RngRange01.Value = "" 'Cover the whole list

    'This If is to select the right data to insert (originally it was more complicated
    'so i've cut it for this explanation)
    If RngRange01.Offset(0, 1).Value <> RngRange01.Offset(-1, 1).Value Then
        ComboBox3.AddItem RngRange01.Offset(0, 1).Value
    End If

    Set RngRange01 = RngRange01.Offset(1, 0) 'Next cell of the list
Loop

'Now we can remove the rows of the combobox we didn't remove previously (if the combobox 
'wasn't empty already)
If BlnCheck = True then
    For IntCounter01 = ComboBox3.ListRows To 1 Step -1
        ComboBox3.RemoveItem IntCounter01 - 1
    Next IntCounter01
End If

End Sub

So far it will work only after the really first time you focus the combobox. Still annoying! To completely remove (trick) the bug you have to add some lanes to the combobox before you focus it; perhaps when you open the workbook like this:

Private Sub Workbook_Open()

Dim IntCounter01 As Integer 'A counter

IntCounter01 = 1 'Set the counter

'In this For-Next we add a lane till we reach the ListRows
For IntCounter01 = IntCounter01 To Sheets("MySheet2").ComboBox3.ListRows
    Sheets("MySheet2").ComboBox3.AddItem ""
Next IntCounter01

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM