Thread: Not Nothing
View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
ArthurJ ArthurJ is offline
external usenet poster
 
Posts: 32
Default Not Nothing

'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