View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Patrick Molloy[_2_] Patrick Molloy[_2_] is offline
external usenet poster
 
Posts: 1,298
Default Using Find & FindNext in a form

Add a listbox (defaults to listbox1) and add the code below to the userform
code, replacing your code.

I can see that you have been reading help, but had a few issues.
First, you loop through the cells using find, but you try to populate text
boxes at the end, after the loop. The code I wrote populates a list box
during the loop.
What you need to add is a listbox click event that will populate the two
text boxes

Option Explicit
Private Sub CommandButton1_Click()

Dim Loc As String
Dim found As Range

Loc = Format(TextBox1.Value, "####")
ListBox1.Clear

With Worksheets("Sheet1")

Set found = .Cells.Find(What:=Loc)

If Not found Is Nothing Then
Loc = found.Address
Do
ListBox1.AddItem .Cells(found.Row, 2)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(found.Row,
3)

Set found = .Cells.FindNext(found)

Loop While found.Address < Loc
End If
End With

End Sub


Private Sub ListBox1_Click()
With ListBox1
TextBox2 = .List(.ListIndex, 0)
TextBox3 = .List(.ListIndex, 1)
End With
End Sub

Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 2
End Sub




"BernzG" wrote:


Hi,

Can someone please help me here - trying to get Find and FindNext to
work within to populate data within a form to enable it to be edited.


Private Sub CommandButton1_Click()

Dim Loc As String

Loc = Format(TextBox1.Value, "####")

Sheets("Sheet1").Select
Cells.Find(What:=Loc, After:=ActiveCell, LookIn:=xlValues, LookAt:=
_
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Activate


With Worksheets("Sheet1")
Set C = .Range("A:A").Find(Loc, LookIn:=xlValues)

If Not C Is Nothing And .Cells(C.Row, 1) < Loc Then firstAddress =
C.Address
Do
Set C = .Range("A:A").FindNext(C)
Loop While Not C Is Nothing And .Cells(C.Row, 1) < Loc And
C.Address < firstAddress

TextBox2 = .Cells(C.Row, 2)
TextBox3 = .Cells(C.Row, 3)
End With

End Sub

Cheers,
Bernz


--
BernzG
------------------------------------------------------------------------
BernzG's Profile: http://www.excelforum.com/member.php...o&userid=22949
View this thread: http://www.excelforum.com/showthread...hreadid=395711