ipbr21054
Well-known Member
- Joined
- Nov 16, 2010
- Messages
- 5,699
- Office Version
- 2007
- Platform
- Windows
Hi,
I have a userform which has various textboxes that search certain columns on my worksheet.
One called TextBoxVehicle searches column D
I type PEUGEOT 308 & i see 3 results appear in the Listbox.
The 3 results are as follows,
VICTORIA HUDSON
HAYDEN WILLIAMS
ANDY WYATT
So i select VICTORIA HUDSON & the cell in column D is selected, which is good.
BUT
I then open the userform again & type PEUGEOT 308 but this time i select HAYDEN WILLIAMS & ANDY WYATT is selected ?
I do the same again & select HAYDEN WILLIAMS & ANDY WYATT is selected ?
I dont see why,
I have supplied the code for TextBoxVehicle & ListBox1
I have a userform which has various textboxes that search certain columns on my worksheet.
One called TextBoxVehicle searches column D
I type PEUGEOT 308 & i see 3 results appear in the Listbox.
The 3 results are as follows,
VICTORIA HUDSON
HAYDEN WILLIAMS
ANDY WYATT
So i select VICTORIA HUDSON & the cell in column D is selected, which is good.
BUT
I then open the userform again & type PEUGEOT 308 but this time i select HAYDEN WILLIAMS & ANDY WYATT is selected ?
I do the same again & select HAYDEN WILLIAMS & ANDY WYATT is selected ?
I dont see why,
I have supplied the code for TextBoxVehicle & ListBox1
Rich (BB code):
Private Sub TextBoxVehicle_Change()
TextBoxVehicle = UCase(TextBoxVehicle)
Dim R As Range, f As Range, cell As String, added As Boolean
Dim sh As Worksheet
Set sh = Sheets("DATABASE")
sh.Select
With ListBox1
.Clear
.ColumnCount = 3
.ColumnWidths = "210;260;80;"
If TextBoxVehicle.Value = "" Then Exit Sub
Set R = Range("D6", Range("D" & Rows.Count).End(xlUp))
Set f = R.Find(TextBoxVehicle.Value, LookIn:=xlValues, LookAt:=xlPart)
If Not f Is Nothing Then
cell = f.Address
Do
added = False
For i = 0 To .ListCount - 1
Select Case StrComp(.List(i), f.Value, vbTextCompare)
Case 0, 1
.AddItem f.Value, i
.List(i, 1) = f.Offset(, -3).Value
.List(i, 2) = f.Offset(, -2).Value
added = True
Exit For
End Select
Next
If added = False Then
.AddItem f.Value
.List(.ListCount - 1, 1) = f.Offset(, -3).Value
.List(.ListCount - 1, 2) = f.Offset(, -2).Value
End If
Set f = R.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
TextBoxSearch = UCase(TextBoxSearch)
.TopIndex = 0
Else
MsgBox "NO ITEM WAS FOUND USING THAT INFORMATION", vbCritical, "DATABASE SHEET ITEM SEARCH"
TextBoxVehicle.Value = ""
TextBoxVehicle.SetFocus
End If
End With
End Sub
Rich (BB code):
Private Sub ListBox1_Click()
Cells.Find(What:=ListBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Unload DatabaseSearchForm
End Sub