【问题标题】:Creating type search sensitive combobox in VBA for Excel在 VBA for Excel 中创建类型搜索敏感组合框
【发布时间】:2019-10-29 01:16:43
【问题描述】:

我是 VBA 新手。一般来说,我会使用Excel函数来处理我自己的内部分析等。但是,在一个资源稀缺的新创业公司,我需要创建一个用户,用户可以从中搜索列表职业,当找到时,它应该在文本框中显示附加到该职业的参数列表,除了输入要搜索的新职业之外,不能更改这些值。

我从我的电子表格开始(4610 行数据,每行旁边有 11 列数据)。我尝试创建的是一个 alpha 敏感类型搜索组合框,用户应该开始输入他们正在搜索的职业,它应该列出 8 个职业作为用户类型,然后他可以选择正确的一个,然后显示另一个用户表单上的参数。

我从用户表单开始,从工具中选择了 ComboBox 和其他应该显示数据的文本框。

我进入 ComboBox 的 Properties 并将 MatchEntry 设置为 1 - fmMatchEntryComplete 和 ControlTipText 开始输入您正在寻找的职业并将 ListRows 设置为 8。

我在 StackOverflow 上搜索了指导并修改了 the following thread 中显示的代码?

这是我的 ComboBox 代码:

Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim x, dict
Dim i As Long
Dim str As String

Set ws = Sheets("Occupations")
Set Rng = ws.Range(Cell1:="C2")


x = ws.Range("C2").Value
Set dict = CreateObject("scripting.dictionary")
str = Me.ComboBox1.Value
If str <> "" Then
    For i = 1 To UBound(x, 1)
        If InStr(LCase(x(i, 1)), LCase(str)) > 0 Then
            dict.Item(x(i, 1)) = ""
            End If
        Next i
        Me.ComboBox1.List = dict.Keys
Else
    Me.ComboBox1.List = x
End If
Me.ComboBox1.DropDown


End Sub

我的职业列表(4610 个条目)位于工作表职业的 C2 列中。

UserForm_Initialise 的代码如下:



Dim occupationName As Range

Dim ws As Worksheet
Set ws = Worksheets("Occupations")


  For Each occupationName In ws.Range(Cell1:="C2")
   With Me.ComboBox1
    .AddItem occupationName.Value
    .List(.ListCount - 1, 1) = occupationName.Offset(0, 1).Value
   End With
 Next occupationName

Me.ComboBox1.SetFocus
Me.ComboBox1.Value = "Type text to open a list of choices"

End Sub

当我运行代码时,我得到一个

运行时错误 '94' 无效使用 Null

str = Me.ComboBox1.Value

我尝试了其他几个线程,也尝试使用 Nz 表示法,但 Excel VBA 无法识别它。

任何帮助将不胜感激。我可以进一步摆弄,仍然可以通过其他方式学习,但不幸的是,这个解决方案已经没有时间了。

【问题讨论】:

  • 试试改成str = Me.ComboBox1.Value &amp; ""

标签: excel vba combobox


【解决方案1】:

我认为您没有正确初始化脚本字典。新语句丢失。 所以脚本字典从来没有完成它自己的隐藏设置并且会失败。

可能在模块头部的某个地方

  dim dict as scripting.dictionary
  set dict = NEW scripting.dictionary 'has to be a new ones ;)


  sub dictionary_reset(dict as scripting.dictionary) 'reset the created dictionary
    on error resume next
    err.clear
    on error resume next
     dict.clearAll
     if err.number <>0 then  debug.print err.description 
  end sub 

在这里,您可以在您的范围内运行并覆盖现有条目或添加新条目。 如果你想从头开始使用 dictionary_reset sub 首先

  function addentry(dict as scripting.dictionary key as string ,entry as string ) as 
   boolean 'this you will need very often...
    addentry=false
    on error resume next
    err.clear
     if not dict.exist(key) then
        dict.item(key)=entry;
     else
        dict.add key , entry 
     end if
        if err.number <>0 then  debug.print err.description 
     addentry=err.number=0
  end function

如果找到密钥,这里将变为 true。结果将在条目中。所以你知道在采取其他行动之前你可以等待什么。有时很方便。

  function searchentry(dict as scripting.dictionary key as string ,entry as string ) as 
        boolean
     searchentry=false
     err.clear 
     on error resume netx
     if dict.exists(key) then 
      searchentry=true
      entry=dict.item(key)
     endif
    if err.number <>0 then  debug.print err.description
  searchentry =err.number=0
  end function  

想尽可能多地使用已知的excel是可以理解的。 通过一些练习,你会走向相反的方向。想想excel中的“IF”;) 可能是一个相反的想法。它还使代码在其他应用程序中可重用。外包这些功能也是一个好主意。然后,您可以通过没有 gui 的测试例程轻松调试它们。最重要的是它们可以重复使用。 我通常还会添加一些错误处理的东西。

顺便说一句:VBA 也就是 VB6。如果您在 VBA 中找不到代码,您可能会找到 VB6 的代码。

在这里玩得开心,享受 VBA :)

【讨论】:

    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2017-03-31
    相关资源
    最近更新 更多