【问题标题】:Sometimes the ActiveX Combobox only shows one row, why?有时 ActiveX 组合框只显示一行,为什么?
【发布时间】:2017-07-12 05:50:16
【问题描述】:

似乎当我第一次点击组合框然后点击箭头时,所有项目都显示出来了。

如果我点击箭头而不点击之前的组合框,则只显示一个项目,我可以点击滚动按钮查看其他项目。

为什么会这样?

这是我用来用项目填充组合框的宏

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 属性。如果您将其设置为BudgetDropdown,则该列表应自动填充。
  • 这听起来是个好主意。但是,ComboBox1.ListFillRange = wConfig.Range("BudgetDropdown") 会抛出错误,提示“类型不匹配”
  • 您应该使用ComboBox1.ListFillRange = "BudgetDropdown",或者在属性编辑器中将其设置为BudgetDropdown。只需确保名称是在工作簿范围内定义的,或者在同一个工作表中。
  • 很好,这很有效,可以解决我的问题。谢谢!

标签: vba excel activex


【解决方案1】:

要使用命名范围内的数据自动填充组合框,请将其ListFillRange 属性设置为范围名称。

你可以在运行时做:

ComboBox1.ListFillRange = "BudgetDropdown"

或者通过在属性窗口中将其设置为BudgetDropdown

【讨论】:

  • 这似乎有一个缺点。每次更改工作表上的值时,都会触发 Combobox1_change 事件。 mrexcel.com/forum/excel-questions/…
  • @user1283776 你对BudgetDropdown 的定义是什么 - 它是否像链接线程中那样易变?
  • 这是 Excel 中的一个命名范围,由 offset 和 counta 函数组成。也许对我来说更好的解决方案是删除组合框的所有项目,然后像我一样阅读它们?但是在我的原始代码中,我遇到的问题 that.clear 似乎不仅删除了所有项目,而且还更改了我不希望这样做的组合框的值/文本。是否可以删除所有项目而不影响组合框中写入的值/文本
  • 我会先用 =$A$1:INDEX($A:$A,COUNTA($A:$A)) 之类的结构替换 OFFSET volatile 范围 - 这应该可以消除不需要的更改事件。
  • 我喜欢您建议的公式,并且将来会在其他情况下使用它。但即使使用新公式,combobox1_change 也会由工作簿中的某些操作触发。我不能将此解决方案用于我的预期目的。我仍然感谢您的帮助和想法!
【解决方案2】:

不知道具体原因,但基本上当您打开一个刚刚清除的组合框时,它只会显示 1 条车道,因为即使您刚刚重新填充它,它也会“认为”它是空的。要欺骗它,您必须逐个删除所有行,直到达到 ListCount 值(组合框将显示的行数)。然后您可以添加所有新行,然后您可以删除以前省略的行。就我而言,我无法使用 ListFillRange,因为我的列表被“嵌入”在多个单元格中;所以我不得不提取它。像这样的:

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

到目前为止,它只有在您真正第一次聚焦组合框后才会起作用。还是很烦!要完全删除(技巧)错误,您必须在集中之前向组合框添加一些通道;也许当您像这样打开工作簿时:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    相关资源
    最近更新 更多