在Excel中使用VBA填充列表时,自动填写下拉列表

我正在使用下面的代码插入数据到另一个工作表的下拉列表中。 这是在用户从另一个下拉列表中select某个选项时实现的。

lstRow = Sheets("Data Sheet").Range("D" & Rows.Count).End(xlUp).Row Sheets("Data Insert").Range("C3").Select With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="='Associated British Ports'!$G$7:$G" & lstRow .IgnoreBlank = False .InCellDropdown = True .InputTitle = "" .ErrorTitle = "Invalid Selection" .InputMessage = "" .ErrorMessage = _ "Please select a user from the list or select New User as the configuration type." .ShowInput = True .ShowError = True End With 

我想添加的function,当用户input几个字母,它通过列表search,并消除任何不包含的东西。 IE说我有下面的下拉列表:托马斯·C·史密斯格雷厄姆·埃文斯的戴维斯·马修斯

而用户input“th”剩余的值应该是

托马斯C史密斯B马修斯

即使是一个简单的版本,用户必须以A的formsinput名称….要返回托马斯将是好的,如果以上是不可行的。

我已经看到这个http://www.ozgrid.com/Excel/autocomplete-validation.htm和这个Excel数据validation与build议/自动完成

但我不认为我不知道如何与上面的代码整合,甚至可能!

任何人都可以帮我吗?

TIA 🙂

这是我的SAYT(Search As You Type)function。 我的表单有一个列表框控件,带有用户列表和一个可用于search列表的文本框控件。

 Private Sub txtSearch_Change() Dim x As Integer lstUsers.ListIndex = -1 For x = 0 To lstUsers.ListCount - 1 lstUsers.ListIndex = x If InStr(1, LCase(lstUsers.Text), LCase(txtSearch.Text), vbTextCompare) > 0 _ Or InStr(1, LCase(lstUsers.List(x, 1)), LCase(txtSearch.Text), vbTextCompare) > 0 _ Then Exit Sub End If Next x End Sub Private Sub txtSearch_KeyPress(ByVal KeyAscii As msforms.ReturnInteger) If KeyAscii = 13 Then txtSearch.Text = lstUsers.Text End If End Sub 

在你input的时候,txtSearch_Change事件触发每一个击键,并且遍历列表框值直到find第一个匹配并select它。 我们还检查K​​eyPress事件以查看用户是否按Enter(ASCII 13)来自动完成search。 我是不区分大小写的(我是LCase的一切),但是你可以很容易地修改它是区分大小写的(甚至可以添加一个checkbox,这样用户可以select区分大小写!)