Find/ Search Not Working Correctly

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I am having a lot of difficulty with the following.

Basically what happens is I have a userform named search, in textbox1 of the search form you type a part number in and it searchs and then fills the appropriate textboxes with the found results.

It then searchs again for the same value within textbox1 from the next cell in the next row to the bottom of the page, if it finds the part again then it will put the appropriate data in the appropriate textboxes, however if it does not find the value then the textboxes would be blank.

What is going wrong is the script is over-riding the activcell to end range and searching from the start again and putting the first found results in textbox6 whereas there should be no found results.

Code:
Private Sub CommandButton2_Click()
Range("A1").Select
On Error Resume Next
Cells.Find(What:=Search.TextBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Search.TextBox3.Value = ActiveCell.Value
ActiveCell.Offset(0, 2).Select
Search.TextBox4.Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Search.TextBox5.Value = "$ " & ActiveCell.Value
 
If Search.TextBox3.Value = "" Then
MsgBox "No Results Found"
Search.TextBox3.Value = ""
Search.TextBox4.Value = ""
Search.TextBox5.Value = ""
End If

second script to fill textbox6.

Code:
Call partfind2
End Sub
Sub partfind2()
ActiveCell.Offset(0, -3).Select
Search.TextBox18.Value = ActiveCell.Address
ActiveCell.Offset(1, 0).Select
ActiveCell.Select
 
Set TopCell = ActiveCell
Set BottomCell = Cells(65535, ActiveCell.Column)
Range(TopCell, BottomCell).Select
Cells.Find(What:=Search.TextBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
 
Search.TextBox6.Value = ActiveCell.Value
 
ActiveCell.Offset(0, 2).Select
Search.TextBox7.Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Search.TextBox8.Value = "$ " & ActiveCell.Value
 
If Search.TextBox6.Value = "" Then
Search.TextBox6.Value = ""
Search.TextBox7.Value = ""
Search.TextBox8.Value = ""
End If
'Call partfind3
End Sub

help would be appreciated
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Without studying your code, my first impression is your problem is probably being caused by using "Select" and "Activecell" together. When you select a cell in code it probably becomes the new "Activecell" just like it would if you made the selection in the GUI. That would seem to indicate that your starting point may keep moving in your "Find" statement.

You do not have to "select" a cell to access its data in code.

Gary
 
Upvote 0
Thanks Gary,

I tried changing the select to activate but it still did the same thing.

I have rewritten my code using additional code and now it works, so I'm happy.

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top