Clear TextBox value but have values shown in listbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
6,092
Office Version
  1. 2024
Platform
  1. Windows
I open my user form & my list box is populated with part numbers.
As i type in my Text Box the list box updates with what im typing.

What i am trying to do is once the search is complete carry out another search so it the looks like the image supplied
So i need to clear the value in the current Text Box.

If i use TextBox1.Value = "" it clears the Textbox & List box values are gone totaly.

If i use TextBox1.Value = 0 it clears the text that was there & replaces it with a 0 & the list box shows all values with 0

What am i doing wrong.

PLEASE ADVISE





Codes in use to help you assist me.
Rich (BB code):
Private Sub UserForm_Initialize()
    myList = Range("Table6")
    ListBox1.List = myList
    TextBox1.SetFocus
End Sub

Rich (BB code):
Private Sub ResetFields_Click()
TextBox1.Value = ""
End Sub
 

Attachments

  • EaseUS_2025_03_29_11_36_29.jpg
    EaseUS_2025_03_29_11_36_29.jpg
    74.4 KB · Views: 5
If i use TextBox1.Value = "" it clears the Textbox & List box values are gone totaly.
Do you mean that the list remains empty?
Can you post your search code and the Textbox_change code.
My approach would be to load the original list, without any searching or filtering, when you clear the textbox.
So something like:
VBA Code:
private sub Textbox1_change()
if me.textbox1.value = "" then
me.listbox1.list = original_list
else
me.textbox1.list = someFilterFunction (original_list, me.textbox1.value)
end if
end sub
 
Upvote 0
I mean using TextBox1.Value = "" the Textbox removes all what was in it.
The listbox is just gone as if it wasnt even on the form
See image supplied.

All code supplied.


Rich (BB code):
Option Explicit

Private myList() As Variant

Private Sub ListBox1_Click()
Dim Cnt As Integer, lastRow As Integer
With Sheets("DATABASE")
    lastRow = .Range("F" & .Rows.Count).End(xlUp).Row
End With
For Cnt = 2 To lastRow
If Sheets("DATABASE").Range("F" & Cnt).Value = PNForm.ListBox1.Text Then
ImageBox.Picture = LoadPicture(CStr(Sheets("DATABASE").Range("G" & Cnt).Value))
Exit For
End If
Next Cnt
End Sub
Private Sub ResetFields_Click()
TextBox1.Value = ""
End Sub

Private Sub TextBox1_Change()
       TextBox1 = UCase(TextBox1)
    Static NoMatch As Boolean
    
    If Len(TextBox1) > 0 Or NoMatch Then
        NoMatch = False
        ListBox1.Visible = True
        ListBox1.List = GetCutList()
        If IsEmpty(ListBox1.List(0)) Then
            MsgBox "NO SUCH NUMBER FOUND", vbCritical, "HONDA EPC NUMBER CHECK"
            ListBox1.List = myList
            NoMatch = True
            TextBox1 = vbNullString
            TextBox1.SetFocus
        End If
    Else
        ListBox1.Visible = False
    End If

End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case Len(TextBox1)
        Case 5, 9
            With TextBox1
                .Text = .Text & "-"
                .SelStart = Len(.Text)
            End With
    End Select
End Sub


Private Function GetCutList() As Variant()
    Dim i As Long
    Dim ret() As Variant
    Dim ret2() As Variant
    Dim x As Long
    
    ReDim ret(UBound(myList, 1), 0)
    For i = 1 To UBound(myList, 1)
        If myList(i, 1) Like "*" & TextBox1 & "*" Then
            ret(x, 0) = myList(i, 1)
            x = x + 1
        End If
    Next
    
    If x > 0 Then
        ReDim ret2(x - 1, 0)
        For i = 0 To x - 1
            ret2(i, 0) = ret(i, 0)
        Next
        GetCutList = ret2
    Else
        GetCutList = Array(Empty, Empty)
    End If
End Function

Private Sub CloseForm_Click()
Unload PNForm
End Sub

Private Sub UserForm_Initialize()
    myList = Range("Table6")
    ListBox1.List = myList
    TextBox1.SetFocus
End Sub
 

Attachments

  • EaseUS_2025_03_29_14_06_11.jpg
    EaseUS_2025_03_29_14_06_11.jpg
    25.7 KB · Views: 2
Upvote 0
The below seems to work for me.
Thanks

Rich (BB code):
Private Sub ResetFields_Click()
TextBox1.Value = ""
ListBox1.List = myList
TextBox1.SetFocus
End Sub

Private Sub TextBox1_Change()
       TextBox1 = UCase(TextBox1)
    Static NoMatch As Boolean
    
    If Len(TextBox1) > 0 Or NoMatch Then
        NoMatch = False
        ListBox1.Visible = True
        ListBox1.List = GetCutList()
        If IsEmpty(ListBox1.List(0)) Then
            MsgBox "NO SUCH NUMBER FOUND", vbCritical, "HONDA EPC NUMBER CHECK"
            ListBox1.List = myList
            NoMatch = True
            TextBox1 = vbNullString
            TextBox1.SetFocus
     End If
     End If

End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top