Error Not Displaying, Instead macro just ends

G

Guest

I have this code which runs, it encounters an error, then the program just
stops. It doesn't give me any messages. I had to use On error goto ErrH,
ErrH: Debug.Print Err to find out there was an Error. The error was 424,
Object undefined. I even put On error goto 0 before the code, but it still
doesn't display a error message for me.
 
G

Guest

Apparently the code causing problems was the following:

If Not Cel2 is Nothing then
.......
endif

Cel2 was Uninitialized. I only had Public Cel2 in the beginning. However I
have Option Explicit, so how come no error came of this?
 
T

Trevor Shuttleworth

As far as I know, "Public Cel2" would be equivalent to "Dim Cel2 as
Variant".

Hence the Option Explicit would have no complaint.

If you haven't initialised it, it would contain nothing so it would execute
the code.

Regards

Trevor
 
D

Dave Peterson

If you used:

Public Cel2 as range
'or even
'Public cel2 as object

Then you could check to see if that range object (or any object) is nothing
with:

if Not (cel2 is nothing) then



But using
Public Cel2 'as Variant
then cel2 would be empty

so you'd use:
if cel2 = empty then

But it's much better to declare your variables with the correct type (As Range,
As Long, As Worksheet, ...)

Because if you assign a range to that variable:

dim cel2 as variant
set cel2 = activesheet.range("a1")
if cel2 = empty then

would be testing to see if the value of the cell is empty.
 
G

Guest

That's what I thought. If it was nothing, then the code should continue. But
the whole macro stops at the "If Not Cel2 is Nothing then" line, which is
what is confusing me.
 

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