有时ActiveXcombobox只显示一行,为什么?
看来,当我第一次点击combobox,然后单击箭头,所有项目都显示。
如果我点击箭头之前没有点击combobox,只显示一个项目,我可以点击滚动button来查看其他项目。
为什么会发生?
这里是我用来填充项目combobox的macros
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
要使用命名范围中的数据自动填充combobox,请将其ListFillRange
属性设置为该范围的名称。
你可以在运行时做到这一点:
ComboBox1.ListFillRange = "BudgetDropdown"
或者通过在属性窗口BudgetDropdown
其设置为BudgetDropdown
。
不知道究竟是为什么,但基本上当你打开一个刚刚清理的combobox时,它只显示一条车道,因为即使你刚填满它,它也会“空想”它是空的。 要欺骗它,你必须删除所有的行1到1,直到你到达你的ListCount值(你的combobox将显示的行数)。 然后你可以添加所有的新行,然后你可以删除以前省略的行。 在我的情况下,我不能使用ListFillRange,因为我的列表“embended”在几个单元格; 所以我不得不提取它。 像这样的东西:
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
到目前为止,它只会在第一次集中combobox之后才能工作。 仍然烦人! 要彻底删除(欺骗)错误,您必须在将焦点添加到combobox之前添加一些通道; 也许当你像这样打开工作簿时:
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