gpt4 book ai didi

excel - 仅使用文本而不使用数字 ComboBox Excel VBA 自动完成

转载 作者:行者123 更新时间:2023-12-03 03:48:56 25 4
gpt4 key购买 nike

假设我在 Excel VBA 中有一个包含以下信息的组合框:

1234 apples
2345 pears
2367 oranges

我希望用户能够输入“a”或“ap”或“app”等。这样自动完成将建议 1234 个苹果。截至目前,只有当用户键入条目的开头部分(即在我的例子中为数字)时,组合框才会自动完成。谢谢

最佳答案

由于您使用的是表单和表单控件,我想引入另一个解决方案(我个人更喜欢)。在本例中,我没有使用组合框,而是使用列表框:

enter image description here

这是填充表单上的列表框并显示表单的代码:

Sub Button3_Click()

Dim i As Long
Dim lngLastRow As Long

Load frmSearchForChoices
With ThisWorkbook.Worksheets(1)
lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lngLastRow
frmSearchForChoices.lstAvailableOptions.AddItem
frmSearchForChoices.lstAvailableOptions.List(frmSearchForChoices.lstAvailableOptions.ListCount - 1, 0) = .Cells(i, 1).Value2
frmSearchForChoices.lstAvailableOptions.List(frmSearchForChoices.lstAvailableOptions.ListCount - 1, 1) = .Cells(i, 2).Value2
frmSearchForChoices.lstAvailableOptions.List(frmSearchForChoices.lstAvailableOptions.ListCount - 1, 2) = .Cells(i, 3).Value2
Next i
frmSearchForChoices.Show
End With

End Sub

以下代码位于表单本身:

Option Explicit
Option Compare Text

Private Sub btnCancel_Click()

frmSearchForChoices.Hide
Unload frmSearchForChoices

End Sub

Private Sub btnOK_Click()

Dim lngMatch As Long

If frmSearchForChoices.lstAvailableOptions.ListCount > 0 Then
If frmSearchForChoices.lstAvailableOptions.ListIndex >= 0 Then
For lngMatch = 0 To frmSearchForChoices.lstAvailableOptions.ListCount - 1
If frmSearchForChoices.lstAvailableOptions.Selected(lngMatch) = True Then
MsgBox "You selected" & Chr(10) & _
frmSearchForChoices.lstAvailableOptions.List(lngMatch, 1) & " (" & _
frmSearchForChoices.lstAvailableOptions.List(lngMatch, 0) & ")" & _
IIf(Len(frmSearchForChoices.lstAvailableOptions.List(lngMatch, 2)) > 0, _
" from " & frmSearchForChoices.lstAvailableOptions.List(lngMatch, 2), "")
frmSearchForChoices.Hide
Unload frmSearchForChoices
End If
Next lngMatch
End If
End If

End Sub

Private Sub txtSearchTerm_Change()

Dim i As Long
Dim lngMatch As Long
Dim varArray As Variant

If Len(Trim(frmSearchForChoices.txtSearchTerm.Value)) = 0 Then Exit Sub

For lngMatch = 0 To frmSearchForChoices.lstAvailableOptions.ListCount - 1
frmSearchForChoices.lstAvailableOptions.Selected(lngMatch) = False
frmSearchForChoices.lstAvailableOptions.List(lngMatch, 3) = 0
Next lngMatch


varArray = Split(Trim(frmSearchForChoices.txtSearchTerm.Value), " ")
For i = LBound(varArray) To UBound(varArray)
For lngMatch = 0 To frmSearchForChoices.lstAvailableOptions.ListCount - 1
If InStr(1, frmSearchForChoices.lstAvailableOptions.List(lngMatch, 1), varArray(i)) Or _
InStr(1, frmSearchForChoices.lstAvailableOptions.List(lngMatch, 2), varArray(i)) Then
frmSearchForChoices.lstAvailableOptions.List(lngMatch, 3) = Val(frmSearchForChoices.lstAvailableOptions.List(lngMatch, 3)) + 1
End If
Next lngMatch
Next i

For lngMatch = 0 To frmSearchForChoices.lstAvailableOptions.ListCount - 1
If frmSearchForChoices.chkMatchBoth.Value Then
If Val(frmSearchForChoices.lstAvailableOptions.List(lngMatch, 3)) >= UBound(varArray) - LBound(varArray) + 1 Then
frmSearchForChoices.lstAvailableOptions.Selected(lngMatch) = True
End If
Else
If Val(frmSearchForChoices.lstAvailableOptions.List(lngMatch, 3)) >= 1 Then
frmSearchForChoices.lstAvailableOptions.Selected(lngMatch) = True
End If
End If
Next lngMatch

End Sub

我确实希望大多数变量和控件能够在代码中被识别,因为遵循命名约定(以frm开头表示表单,lbl表示表单上的标签, 'lst' 用于表单上的列表框等)。不过,如果您对此解决方案有任何疑问,请随时告诉我。

关于excel - 仅使用文本而不使用数字 ComboBox Excel VBA 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37572688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com