ipbr21054
Well-known Member
- Joined
- Nov 16, 2010
- Messages
- 5,226
- Office Version
- 2007
- Platform
- Windows
Morning,
I am using the code shown below.
I originally only needed to search for a customers name in column A hence the code shown.
I am now finding myself needing to search for other things which are in different columns as my database has become much larger.
Example.
Search for....
Car registration number in column B
Vehicle make in column D
Key code in column J
etc etc
Please can you advise a generic code that would allow me to type in a Textbox for the item to be searched but im not sure how to go about advising which column it should be searched in
I am using the code shown below.
I originally only needed to search for a customers name in column A hence the code shown.
I am now finding myself needing to search for other things which are in different columns as my database has become much larger.
Example.
Search for....
Car registration number in column B
Vehicle make in column D
Key code in column J
etc etc
Please can you advise a generic code that would allow me to type in a Textbox for the item to be searched but im not sure how to go about advising which column it should be searched in
Rich (BB code):
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
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 = 2
.ColumnWidths = "100;0"
If TextBox1.Value = "" Then Exit Sub
Set r = Range("A5", Range("A" & Rows.Count).End(xlUp))
Set f = r.Find(TextBox1.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.Row
added = True
Exit For
End Select
Next
If added = False Then
.AddItem f.Value
.List(.ListCount - 1, 1) = f.Row
End If
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
TextBox1 = UCase(TextBox1)
.TopIndex = 0
Else
MsgBox "NO CUSTOMER WAS FOUND USING THAT INFORMATION", vbCritical, "DATABASE SHEET CUSTOMER NAME SEARCH"
TextBox1.Value = ""
TextBox1.SetFocus
End If
End With
End If
End Sub