Find


Posted by P James on December 17, 2001 8:09 AM

I'm tring to search a list of values in a column through a text box on a user form. Then, when the value is found, to display the values in adjacent cells to be displayed in other text boxes in my form. The code i'm using is this:

Private Sub TextBox1_Change()

With Worksheets("source").Range("a1:a10")
Set c = .Find(TextBox1.Value, LookIn:=xlValues, LookAt:= _
xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
TextBox1a.Value = c.Offset(0, 1).Value
TextBox1b.Value = c.Offset(0, 2).Value
TextBox1c.Value = c.Offset(0, 3).Value
CheckBox1.Value = True
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub

I also have the following code for the checkbox to clear the cells if it is 'un-ticked':

Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
TextBox1.Value = ""
TextBox1a.Value = ""
TextBox1b.Value = ""
TextBox1c.Value = ""
CheckBox1.Value = False
End If
End Sub

I have many rows of these text boxes, all referencing the same data ("a1:a10"). The problems is that it doesn't work the first time the form is loaded on loading the application. Everytime after that, it works - it even works if you go back into the text box that didn't work the first time.

Please advise what I'm doing wrong.

Thanks



Posted by Sandra on December 18, 2001 5:33 AM

Hi there,

Which value you want to display ?
You can use the following macro for displaying the last found row
Private Sub TextBox1_Change()

Dim firstAddress
Dim c
Dim icSearch As Integer
Dim irBegin As Integer
Dim irLast As Integer

With Worksheets("source").Range("a1:a10")
icSearch = 1 ' column A:A
irBegin = 1 ' begin search from rownr = 1
Cells(irBegin, icSearch).Select
Set c = .Find(TextBox1.Value, After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If c.Row <> 1 Then irLast = c.Row
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
TextBox1a.Value = Cells(irLast, icSearch + 1).Value
TextBox1b.Value = Cells(irLast, icSearch + 2).Value
TextBox1c.Value = Cells(irLast, icSearch + 3).Value
CheckBox1.Value = True
Else
TextBox1a.Value = "n.a."
TextBox1b.Value = "n.a."
TextBox1c.Value = "n.a."
End If

End With
End Sub

Hoop it'll help