【问题标题】:unable to populate combobox using .rowsource {vba excel}无法使用 .rowsource {vba excel} 填充组合框
【发布时间】:2017-06-04 15:00:34
【问题描述】:

我是 VBA excel 的新手,我在这里尝试使用工作表中的数据填充用户表单中的组合框。我定义了地址并使用了 .rowsource 属性,其中代码通过没有任何错误,但组合框中没有填充任何内容。任何帮助将不胜感激。

下面是代码-

私有子 empSearchdb_Click()

Dim cell As Range
Dim emptyRow As Long
Dim rng As Range
Dim ws1 As Worksheet
Dim empSearchKeyword As String

'Stop screen flickering
Application.ScreenUpdating = False

'setting worksheet
Set ws1 = Sheets("EmployeeDetails")
ws1.Activate

'find last empty row
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2

'when nothing is selected
If Me.empSearchL.ListIndex = -1 Then

    'from $A$1 to $J$lastrow-1
    Set rng = Range("$A$1", Cells(emptyRow - 1, 10))

    With Me.empDetails
        .RowSource = rng.Address
    End With

新修改的代码-

Dim cell As Range
Dim emptyRow As Long
Dim rng As Range
Dim ws1 As Worksheet
Dim empSearchKeyword As String

'Stop screen flickering
Application.ScreenUpdating = False

'setting worksheet
Set ws1 = ActiveWorkbook.Sheets("EmployeeDetails")

'find last empty row
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2

'from $A$1 to $J$lastrow-1
Set rng = ws1.Range("$A$1", ws1.Cells(emptyRow - 1, 10))
Set rng = ws1.Range("$A$1", "$J$10")


'With your listbox do the following
If Me.empSearchL.ListIndex = -1 Then
    With Me.empDetails
        'If nothing is selected, add a row source
        .ColumnCount = 10 'Otherwise you will only get 1 column
        .RowSource = rng.Address
    End With
End if
Application.ScreenUpdating = True

【问题讨论】:

  • 代码结束后你真的有Application.ScreenUpdating = True吗?您至少应该显示到End If
  • @Noceo 是的,我最后得到了Application.ScreenUpdating = True。很抱歉代码不完整,尽管我还有两个 if 列表索引的条件,但我只关心List Index = -1。所以这段代码就可以了,只是缺少一个End If。

标签: vba excel combobox userform


【解决方案1】:

编辑:我已按回答删除,因为我没有正确理解问题。

【讨论】:

  • 我需要一个名为empDetails 和empSearchL 的组合框是我从中选择条件的列表框。所以简而言之,如果在empSearchL 中选择了任何内容并且用户单击搜索,那么它应该在组合框中显示所有内容。我尝试了上面的代码,但它仍然显示空的组合框。我在下面添加了修改后的代码。
  • 对不起,我以为是列表框,我错了。您可以为控件添加前缀。使用 cboEmpDetails 和 lstEmpSearch 的 MS 标准。
  • 如果列表框中没有选择任何内容,您希望组合框(下拉)显示所有 100 个项目吗?这是很多(!)。如果在列表框中选择了某些内容,那么组合框中应该显示什么?
  • 谢谢,我将使用 MS 编码标准。就列表框而言,是的,我想要所有数据。好吧,如果选择了列表项(条件),那么用户需要输入值来搜索我使用组合框的.value 属性手动获取一行的位置。 (例如,我可以按 ID 或姓名搜索)。因此,如果我能够找出.listbox = -1 ,那么我们可以将其用于其他条件。
  • 但是您建议的新更新代码在组合框中也没有给出任何内容。我已经在上面更新了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多