View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Problem with Loop

Mike,

The problem is that if c is nothing, c.Address will fail with an
error 91. Try something like the following:

Dim C As Range
Dim FirstAddress As String
Dim Done As Boolean

With Worksheets(1).Range("a1:a500")
Set C = .Find(2, LookIn:=xlValues)
If Not C Is Nothing Then
FirstAddress = C.Address
Do
C.Value = 5
Set C = .FindNext(C)

If C Is Nothing Then
Done = True
Else
If C.Address = FirstAddress Then
Done = True
End If
End If

Loop While Done = False
End If
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Mike" wrote in message
...
Hi. The following Loop works well until all of the items
have been replaced. In the final loop, I receive the
following error: "Object variable or With Block Variable
not set."

Any ideas?

Thanks,
Mike.
--------
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <
firstAddress
End If
End With