Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

making a macro to run 'find'

Status
Not open for further replies.

boyracer3000

Programmer
Mar 1, 2002
3
NL
I'd like to create a macro to run the find menu (Ctrl+F). I think it is probably quite simple but don't know the correct code to make it show. I tried recording it but can't stop while the find menu is open.

Ideally I'd like to make a custom form along the lines of

Dim Query As String
Query = txtDetails.Value

Range("A1").Select
Cells.find(What:=Query, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

this has worked when the data is present but when erroneous data is input I get an error message. Any help appreciated thanks.
 
I think the problem is that you are trying to

.Activate

the found cell and it doesn't exist.

The following seems to solve your problem.

Sub Find_abc()

Dim rngFound As Range

Set rngFound = Cells.Find(What:="abc", _
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If rngFound Is Nothing Then
MsgBox "does not occur"
Else
MsgBox "found it at " & rngFound.Address
End If

Exit Sub

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top