Runtime error '91' when trying to exit Do...Loop

C

Cloudfall

I get the "Runtime error '91' " message when trying to exit a
Do...Loop. My code is as follows:

Dim cell As Range
Dim sFirst As String
:
Else
Set cell = Nothing
End If
Loop Until cell Is Nothing Or sFirst = cell.Address

I have stepped through this code. Everything works fine until I get to:

'Else', then 'Set cell = Nothing', 'End If' (the "Locals Window" tells
me that 'cell' contains 'Nothing', 'sFirst' contains "$D$4"), 'Loop
Until cell Is Nothing Or sFirst = cell.Address', and then when I try to
step to the next line it generates:

"Runtime error '91': Object variable or With block variable not set."

The code works if I comment out "Or sFirst = cell.Address" as in the
following line:

Loop Until cell Is Nothing 'Or sFirst = cell.Address

It's hard to imagine that there is something wrong with 'sFirst =
cell.Address' as the following line of code worked earlier in this
program without any problem:

"If Not cell Is Nothing And cell.Address <> sFirst Then"

I really have no idea what is going on here. Does anybody out there
know? Thank you.
 
J

JE McGimpsey

If cell is Nothing, then cell doesn't have an .Address property. Since
both arguments of And are evaluated, you'll always get an error.

Do you need to set cell to Nothing?
 
C

Cloudfall

Thank you for your Fast(!) reply. I probably don't need to set cell to
Nothing. Now that this mystery has been solved, I can more confidently
make changes to the code. If you're an Australian, I hope you win a big
trifecta on the Melbourne Cup tomorrow!!! All the best.
 

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