Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I determine if a chart exists on a worksheet?

875 views
Skip to first unread message

Paul Alappat

unread,
Aug 25, 2000, 3:00:00 AM8/25/00
to
Hello all Excel Gurus,

Can someone tell me how to check and see if a graph exists on a
worksheet so that I can either add another series to it or create a new
chart. In other words, if a chart already exists, I only want to add a
series to it. However, if the chart doesn't exist, I want to create
one. Unfortunately, I don't know the code required to check if a graph,
such as "Chart 1" exists. Can anyone help? Thanks!

Paul


Michael

unread,
Aug 25, 2000, 3:00:00 AM8/25/00
to
Here you go Paul,

This is what I used with XL2000, this code checks for a chart on each sheet
in the active workbook.
I tested the sheet type in case the chart was on a chart sheet instead of
embedded in a worksheet.


Option Explicit

Sub ChartCheck()

Dim msg As VbMsgBoxResult
Dim n As Integer

On Error Resume Next

For n = 1 To Sheets.Count

Sheets(n).ChartObjects(1).Select

If Err <> 0 And Sheets(n).Type <> 4 Then
msg = MsgBox("No chart here!", vbOKOnly, Sheets(n).Name)
Else
msg = MsgBox("Chart already exists!", vbOKOnly, Sheets(n).Name)
End If

Err = 0
Next n

End Sub

Mike


"Paul Alappat" <pala...@cisco.com> wrote in message
news:39A6DA4...@cisco.com...

Mark Roth

unread,
Aug 27, 2000, 3:00:00 AM8/27/00
to
What does the .type <> 4 do?

"Michael" <medi...@hotmail.com> wrote in message
news:i2Gp5.810$HQ5.3...@news.uswest.net...

Michael Edison

unread,
Aug 27, 2000, 3:00:00 AM8/27/00
to
Mark,

> "Michael" <medi...@hotmail.com> wrote in message
> news:i2Gp5.810$HQ5.3...@news.uswest.net...

> > I tested the sheet type in case the chart was on a chart sheet instead


of
> > embedded in a worksheet.

If Sheets(n).Type = 4 then Sheets(n) is a chart sheet.

There is probably an alpha code for this like x1ChartSheet but I did not
find it anywhere

Embedded charts are Worksheet objects and Chart Sheets are Workbook objects.
Chart Sheets do not contain the ChartObject collection, therefore

Sheets("Chart Sheet").ChartObjects(1).Select

would produce an error.


Check out "The Chart object model" pp 472-473 in John Walkenbach's Excel
2000 Power Programming with VBA for more info


Mike

"Mark Roth" <mr_mar...@hotmail.com> wrote in message
news:FP3q5.212368$8u4.2...@news1.rdc1.bc.home.com...

Paul Alappat

unread,
Aug 28, 2000, 3:00:00 AM8/28/00
to
Thanks for the help Michael!

Paul

0 new messages