Find: runtime error 1004 Application-defined or object-defined error

I

inspired

At the "Find" line, I get a runtime error 1004 Application-defined
or object-defined error. I tried Googling this, but I could not find
a solution for this particular occurrence. My code may be found
below.

Can anyone help? Thanks, Alan

Sub ExtractMessages()
Dim row As Long, LastRow As Long, temp As String, MsgRow As Long,
LastMsgRow As Long, FindMsgName As String
Dim pos As Integer, message As String, purpose As String,
initiation As String, trigger As String
Dim msgstriggered As String, MessageWS As Object, MessageListWS As
Object, rng As Range
Set MessageWS = Worksheets("Messages")
Set MessageListWS = Worksheets("MessageList")
LastRow = MessageWS.UsedRange.Rows.Count
LastMsgRow = MessageListWS.UsedRange.Rows.Count
' Turn off screen updates
Application.ScreenUpdating = False
' Look for each message
For MsgRow = 1 To LastMsgRow
FindMsgName = MessageListWS.Cells(MsgRow, 1).Value
FindMsgName = Trim(FindMsgName)
If Len(FindMsgName) > 0 Then
' Find matching message name
With MessageWS.Cells
Set rng = .Find(What:=FindMsgName, After:=.Cells(1,
1), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
End With
If Not (rng Is Nothing) Then rng.Activate
End If
Next MsgRow

Application.ScreenUpdating = True
End Sub
 
D

Dave Peterson

I don't see anything wrong with that line -- and it worked ok in my simple tests.

Can you add any more details of what's in the cells when it fails?
 
I

inspired

Sorry. It finds the text, but then it fails when I try to activate
the range returned from Find.
However, I can print out the cell address of the range.

Alan
 
D

Dave Peterson

You can only activate a range on a sheet that's selected.

I'd use:

If rng Is Nothing Then
'do nothing
else
application.goto rng 'scroll:=true 'or false??
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top