【问题标题】:Reverse ComboBox: How to programmatically select an item in ActiveX ComboBox based on a TextBox value?反向组合框:如何根据文本框值以编程方式选择 ActiveX 组合框中的项目?
【发布时间】:2021-12-13 08:11:24
【问题描述】:

我有一个多列组合框,现在尝试搜索代码,但无济于事。

这是我的组合框的样子:

  emp_id         emp_name
 ---------    ---------------
  <blank>      - Select -
     1         James Bond
     2         Jason Statham
     3         Sylvester S.
     4         Chuck Norris

其中columnwidths = "0,100" and and columnheads = False,所以它只会显示emp_name

我想要做的是,以编程方式选择并显示基于变量的组合框中的项目

到目前为止,与我想要实现的最接近的逻辑是:

empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
    For i = 0 To .ListCount - 1
        If .Column(0, i) = empSysID Then
            .Value = .List(i) '~Trying to select and display 'Sylvester S.' in combobox
            Exit For
        End If
    Next i
End With

但我收到运行时错误“380”:无法设置值属性。无效的属性值。

提前谢谢你!

【问题讨论】:

  • .ListIndex = i?

标签: excel vba combobox


【解决方案1】:

你需要使用ListIndex:

empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
    For i = 0 To .ListCount - 1
        If .Column(0, i) = empSysID Then
            .ListIndex = i 
            Exit For
        End If
    Next i
End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多