Thread: VBA Find Bug
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default VBA Find Bug

The problem most likely is that you append the Activate method to the
Find method. Find returns a Range object pointing to the cell in which
the data was found. However, if the value was not found, Find returns
Nothing, and your code still tries to Activate that. You can't do
anything with a Nothing. A better approach is:

Dim FoundCell As Range
Set FoundCell = Cells.Find(....)
If FoundCell Is Nothing Then
' value not found. do something
Else
' value was found
FoundCell.Activate
End If

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Thu, 27 Aug 2009 11:22:57 -0700 (PDT), jlclyde
wrote:

In a user form I am trying to find the text 1234. As soon as it gets
to the find line it bugs out. Any ideas?

Private Sub Enter_Click()
If DtBx = "" And ItemNum = "" Then
MsgBox "Must Enter Item Number"
ElseIf DtBx = "" Then
Cells.Find(What:="1234", After:=ActiveCell, LookIn:=xlValues,
LookAt:= _
xlWhole, SearchOrder:=xlByColumns,
SearchDirection:=xlPrevious, MatchCase:= _
False, SearchFormat:=False).Activate
End If
End Sub
Thanks,
Jay