VBA:文本框搜索栏和列表框

问题描述 投票:0回答:1

大家的问候,

在我的vba用户表单上,我有一个文本框(比方说Txt1)。我将其用作列表框(Lst1)的搜索栏。

当我将任何表达式写到Txt1时,我可以很容易地找到Lst1内部的内容。这两个(Txt1和Lst1)已集成到我的库存程序中。

当我用Txt1搜索任何产品时,我可以从Lst1中选择产品名称。

[当我从Lst1中选择产品时,我将数量写入Txt2,然后按Enter键。它在程序的背面起作用并进行必要的处理。

我想做的是:当我按Enter键时,Txt1会自行清理。我需要再次将新产品名称重新写入Txt1。

在此过程中,我不想清理Txt1。但是我不知道该怎么办?

有人可以帮我吗?

我在下面提供我的代码:

**Txt1 Code :**
Private Sub TextBox17_Change()

Dim i As Long
Me.TextBox17.Text = StrConv(Me.TextBox17.Text, 1)
Me.ListBox4.Clear
For i = 2 To Application.WorksheetFunction.CountA(Sayfa2.Range("C:C"))
a = Len(Me.TextBox17.Text)
If Left(Sayfa2.Cells(i, 3).Value, a) = Left(Me.TextBox17.Text, a) Then
Me.ListBox4.AddItem Sayfa2.Cells(i, 3).Value
Me.ListBox4.List(ListBox4.ListCount - 1, 3) = Sayfa2.Cells(i, 10).Value
End If
Next i

End Sub

**Lst1 Code :**
Private Sub ListBox4_Click()

Dim i As Long, lastrow As Long
lastrow = Sheets("CDepo").Range("C" & Rows.Count).End(xlUp).Row
            For i = 2 To lastrow
                If CStr(Sheets("CDepo").Cells(i, "C").Value) = (Me.ListBox4.Value) Then
                    Me.TextBox2 = Sheets("CDepo").Cells(i, "A").Value
                    Me.TextBox11 = Sheets("CDepo").Cells(i, "H").Value
                    Me.TextBox9 = Sheets("CDepo").Cells(i, "C").Value
                    Me.ComboBox2 = Sheets("CDepo").Cells(i, "B").Value
                    Me.TextBox6 = Sheets("CDepo").Cells(i, "I").Value
                    Me.TextBox13 = Sheets("CDepo").Cells(i, "J").Value
                   Me.TextBox15 = Sheets("CDepo").Cells(i, "P").Value
                    Me.TextBox8 = Sheets("CDepo").Cells(i, "D").Value
                    Me.TextBox16 = Sheets("CDepo").Cells(i, "Q").Value
                End If
    Next

End Sub

**Commandbutton for Enter :**
Private Sub CommandButton6_Click()

If UserForm11.ComboBox4 = "TesellumFisi" Then

    If TextBox2.Text <> "" Then
        Son_Dolu_Satir = Sheets("TesellumFisi").Range("A65536").End(xlUp).Row

        For i = 23 To Son_Dolu_Satir
            If Sheets("TesellumFisi").Cells(i, 2).Value = TextBox2.Text Then
                Exit Sub
            End If
        Next i
            Bos_Satir = Son_Dolu_Satir + 1
                Sheets("TesellumFisi").Range("A" & Bos_Satir).Value = Application.WorksheetFunction.Max(Sheets("TesellumFisi").Range("A:A")) + 1
                    Sheets("TesellumFisi").Range("B" & Bos_Satir).Value = TextBox2.Text
                    Sheets("TesellumFisi").Range("C" & Bos_Satir).Value = TextBox13.Text
                    Sheets("TesellumFisi").Range("E" & Bos_Satir).Value = TextBox9.Text
                    Sheets("TesellumFisi").Range("I" & Bos_Satir).Value = TextBox4.Text
                    Sheets("TesellumFisi").Range("J" & Bos_Satir).Value = TextBox6.Text
                    Sheets("TesellumFisi").Range("O13").Value = UserForm11.ComboBox1.Text
                    Sheets("TesellumFisi").Range("H" & Bos_Satir).Value = UserForm11.ComboBox2.Text
                    Sheets("TesellumFisi").Range("G" & Bos_Satir).Value = TextBox8.Text
    End If
End If

Dim TC As Control
    For Each TC In Controls
        If TypeName(TC) = "TextBox" Or TypeName(TC) = "ComboBox" Then
            TC.Value = ""
        End If
    Next TC
    Set TC = Nothing

'--
        TextBox2.SetFocus
        TextBox2.SelStart = 1
        TextBox2.SelLength = Len(TextBox2.Text)

End Sub

感谢鳍前进。

excel vba textbox listbox enter
1个回答
0
投票

要跳过Text1但清除其余文本框,则需要在控件循环中使用条件。

Dim TC As Control
    For Each TC In Controls
        If TypeName(TC) = "TextBox" Or TypeName(TC) = "ComboBox" Then
            If Not TC.name = "TextBox1" then 'Assuming this is the right name
                TC.Value = ""
            end if
        End If
    Next TC
    Set TC = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.