Not Nothing

G

Guest

This works:

If ActiveChart Is Nothing Then
**code**
End If

But what I need is the reverse of this, as in ...

If ActiveChart Is 'Not' Nothing, or
If ActiveChart is 'Something'

Of course neither of these work.

How do I do this?

Art
 
B

Bob Phillips

If Not ActiveChart Is Nothing Then


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
D

Dave Peterson

if not activechart is nothing then
or
if not (activechart is nothing) then
or I'd use:

if activechart is nothing then
'do it's nothing stuff
else
'do if it's "something" stuff
end if

I find the last one much easier to wrap my brain around (I don't like double
negatives <bg>).

Well, except in =sumproduct()'s. (An excel joke!)
 
G

Guest

Thank you Bob and Dave. Just what I needed.

Dave, the reason I didn't use the following structure ....

if activechart is nothing then
'do it's nothing stuff
else
'do if it's "something" stuff
end if

..... is that the 'nothing stuff' is many lines of code
while the 'something stuff' is zero lines. That makes
the If-Then-Else statement really hard to read!

Art
 
D

Dave Peterson

I don't think this looks bad:

if activechart is nothing then
'your lots of code here
end if

===
and I use this all the time:

if something = true then
'do nothing
else
a bunch of code here
end if

I actually put the characters "'Do nothing" in the code--for readability sake
only.

Kind of like "NEXT SENTENCE" in COBOL.
 
G

Guest

'I thought my question was resolved, and it is, well, sort of ....

'Can anyone explain the peculiar behavior of the second macro?

Sub testDeactivate1()
'Crashes if there is no active chart.
Worksheets(2).Activate
ActiveChart.Deselect
End Sub

Sub testDeActivate2()
'This sub does not crash and the end result is that there
'is no active chart.

'But the odd thing is that if there is
'NOTHING selected (including cells) then the macro branches through
'the Then part of the If statement (even though there is no
'active chart). Yet it does not crash!

'Contrast this behavior with the other sub, which crashes if there is
'no active chart.

'If there is a cell range selected, the macro branches through
'the Else portion of the routine.

Worksheets(2).Activate
If Not (ActiveChart Is Nothing) Then
MsgBox ("There IS an active chart")
ActiveChart.Deselect
MsgBox ("Not any more!")
Else
MsgBox ("There is NOT an active chart.")
End If
End Sub
 

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