PDA

View Full Version : Have txtBox search results populated in listbox



Chris65
12-01-2011, 07:42 AM
I work for an oil delivery company. I have to track customer complaints using an Excel 2003 workbook. We use only one sheet in the workbook which has 18 columns and approx 10,000 entries. The complaint numbers are in Column D, listed as "Complaint #" on the worksheet. I want to be able to type a "Complaint Number" in a textbox labeled "txtSearchComplaint" and then click on button labeled "btnSearchComplaint" and then have the search results populated in a listbox labeled "lbxDisplayComplaints." Our complaint numbers will sometimes be followed by marks. For example complaint # 789 may appear as 789, 789*, 789-1, 789-2, 789-*1 - depending on how the complaint is addressed. So all of these variation must be display in the listbox. I am a drooling idiot with VBA. Any help would be much appreciated. Thank you Chris.

Bob Phillips
12-01-2011, 09:05 AM
Why not just autofilter the worksheet?

Chris65
12-01-2011, 10:21 AM
I have no VBA knowledge. I know how to use the filters on the worksheet but how do I get that information into the listbox. Because once I see the complaint number that I am looking in the listbox - I will click on it and have my 18 textboxes populated so I can read/review/update.

Bob Phillips
12-01-2011, 11:46 AM
No, my question is saying why bother with the listbox. Autofilter can show all items that match whatever criteria yoiu set, suc as 'Begins with' and a value of 789

Chris65
12-01-2011, 12:50 PM
Because I have a userform with 18 textbox that enable me to view my entire row of data without scrolling. I also add new data and edit data through this userform. My problem is that I can't populate my textboxes that have "marks" associated with them. All I need is a code to do the aforemention function.

Kenneth Hobs
12-01-2011, 01:58 PM
I would have recommended what xld did.

Change the name Sheet1 in the two places to your sheet name.
Private Sub btnSearchComplaint_Click()
Dim c As Range, f As Range
lbxDisplayComplaints.Clear
For Each c In Worksheets("Sheet1").Range("D2", _
Worksheets("Sheet1").Range("D" & Rows.Count).End(xlUp))
Set f = c.Find(txtSearchComplaint.Value, lookat:=xlPart)
'If InStr(1, c.Value, txtSearchComplaint.Value, vbBinaryCompare) > 0 Then
If Not f Is Nothing Then
lbxDisplayComplaints.AddItem c.Value
End If
Next c
End Sub

Chris65
12-01-2011, 02:34 PM
Thank you so much Mr. Hobs! Your code works perfectly. I've spent so many hours searching different forums and looking at different posts trying to learn how to program this one particular part of my userform. Thank you very much, Chris.