假设我在Excel VBA中有一个ComboBox,其中包含以下信息:
1234 apples
2345 pears
2367 oranges
我希望用户能够键入'a'或'ap'或'app'等。这样,自动完成功能将建议使用1234个苹果。到目前为止,只有当用户键入条目的开头部分(即我的数字)时,组合框才会自动完成。谢谢
所以首先给这个答案一些背景。SectorDropDown1_1(是表单的一部分)是一个下拉列表,其中填充有数字值和来自列J的字符串值的并置字符串。在这种情况下,_change()方法响应用户输入值。我在电子表格“ SectorSearch”中有2列“ R”和“ S”,已将数字和文本分开。因此,现在如果用户输入数字或文本,则将在“下拉列表”中选择适当的值。但这是问题所在,我的代码有点笨拙地跳到了正确的值,因此我想“使事情变得顺畅”,这样一来,用户输入超过2个与适当值匹配的字符后,便可以选择此值下拉列表显示附近的值。
Option Explicit
Private Sub SectorDropDown1_1_Change()
Dim i As Long
Dim StringRange1 As String
Dim StringRange2 As String
Dim Stringrange3 As String
Dim LengthOfValue As Integer
Dim TotalSectorCodes As Integer
If SectorDropDown1_1.Value <> "" And Len(SectorDropDown1_1.Value) > 2 Then
TotalSectorCodes = Worksheets("SectorSearch").Range("J:J").Cells.SpecialCells(xlCellTypeFormulas).Count
LengthOfValue = Len(SectorDropDown1_1.Value)
For i = 2 To TotalSectorCodes
StringRange1 = "R" & CStr(i)
StringRange2 = "S" & CStr(i)
Stringrange3 = "J" & CStr(i)
Select Case SectorDropDown1_1.Value
Case Left(Worksheets("SectorSearch").Range(StringRange1).Value, LengthOfValue)
SectorDropDown1_1.Value = Worksheets("SectorSearch").Range(Stringrange3).Value
Exit For
Case Left(Worksheets("SectorSearch").Range(StringRange2).Value, LengthOfValue)
SectorDropDown1_1.Value = Worksheets("SectorSearch").Range(Stringrange3).Value
Exit For
Case Else
'...
End Select
Next i
End If
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句