PC Review


Reply
Thread Tools Rate Thread

Checking if a Chart Exists on a Sheet

 
 
Keith Wilby
Guest
Posts: n/a
 
      30th Mar 2007
Newbie question:

What code do I need to check if a chart exists on a worksheet?

Many thanks.

Keith.


 
Reply With Quote
 
 
 
 
Keith Wilby
Guest
Posts: n/a
 
      30th Mar 2007
"Keith Wilby" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Newbie question:
>
> What code do I need to check if a chart exists on a worksheet?
>


Whoops, how do I determine it's name if one exists? I've tried
ActiveSheet.ChartObjects.ActiveChart.Name but that's obviously not right.


 
Reply With Quote
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      30th Mar 2007
Hi
Charts are contained inside ChartObjects. Chartobjects have names
(which you don't see on the "chart") while Charts have titles (which
you do see).
If you only have one chart on a sheet then it is contained inside one
ChartObject and you can do

myName = ActiveSheet.ChartObject(1).Name
myTitle = ActiveSheet.ChartObject(1).Chart.Title
to get name and title.

To see if a chart exists try

On Error resume next
Set myChartObject = Activesheet.ChartObjects(myName)
If myChartObject is Nothing then


On Mar 30, 9:05 am, "Keith Wilby" <h...@there.com> wrote:
> "Keith Wilby" <h...@there.com> wrote in message
>
> news:(E-Mail Removed)...
>
> > Newbie question:

>
> > What code do I need to check if a chart exists on a worksheet?

>
> Whoops, how do I determine it's name if one exists? I've tried
> ActiveSheet.ChartObjects.ActiveChart.Name but that's obviously not right.



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      30th Mar 2007
Sub test()
Dim i As Long
Dim s As String
Dim ws As Worksheet

Set ws = ActiveSheet

For i = 1 To ws.ChartObjects.Count
s = s & ws.ChartObjects(i).Name & vbCr
Next

If Len(s) = 0 Then s = "No Charts on " & ws.Name

MsgBox s
End Sub

Regards,
Peter T


"Keith Wilby" <(E-Mail Removed)> wrote in message
news:460cc17a$(E-Mail Removed)...
> "Keith Wilby" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Newbie question:
> >
> > What code do I need to check if a chart exists on a worksheet?
> >

>
> Whoops, how do I determine it's name if one exists? I've tried
> ActiveSheet.ChartObjects.ActiveChart.Name but that's obviously not right.
>
>



 
Reply With Quote
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      30th Mar 2007
Sorry, post went when I pressed return for some reason. Last bit
should be

On Error resume next
Set myChartObject = Activesheet.ChartObjects(myName)
If myChartObject is Nothing then
'some code
End if
On error goto 0

where myName is a text string.
If you have several charts with Titles but the parent chartobjects do
not have names try

For Each Cht In Activesheet.ChartObjects
If Cht.Chart.Title = myTitle then
msgbox "Chart Exists!"
Exit For
end if
next Cht

regards
Paul


On Mar 30, 9:28 am, paul.robin...@it-tallaght.ie wrote:
> Hi
> Charts are contained inside ChartObjects. Chartobjects have names
> (which you don't see on the "chart") while Charts have titles (which
> you do see).
> If you only have one chart on a sheet then it is contained inside one
> ChartObject and you can do
>
> myName = ActiveSheet.ChartObject(1).Name
> myTitle = ActiveSheet.ChartObject(1).Chart.Title
> to get name and title.
>
> To see if a chart exists try
>
> On Error resume next
> Set myChartObject = Activesheet.ChartObjects(myName)
> If myChartObject is Nothing then
>
> On Mar 30, 9:05 am, "Keith Wilby" <h...@there.com> wrote:
>
>
>
> > "Keith Wilby" <h...@there.com> wrote in message

>
> >news:(E-Mail Removed)...

>
> > > Newbie question:

>
> > > What code do I need to check if a chart exists on a worksheet?

>
> > Whoops, how do I determine it's name if one exists? I've tried
> > ActiveSheet.ChartObjects.ActiveChart.Name but that's obviously not right.- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
Keith Wilby
Guest
Posts: n/a
 
      30th Mar 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

Many thanks Paul (and Peter).

Regards,
Keith.


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      30th Mar 2007
One more:

If ActiveSheet.ChartObjects.Count > 0 Then
MsgBox "has some!"
Else
MsgBox "nope"
End If

Keith Wilby wrote:
>
> Newbie question:
>
> What code do I need to check if a chart exists on a worksheet?
>
> Many thanks.
>
> Keith.


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking if a sheet exists greg Microsoft Excel Programming 6 9th Jul 2008 09:58 PM
Checking if the sheet already exists =?Utf-8?B?R3JlZw==?= Microsoft Excel Programming 3 9th Nov 2006 01:25 AM
Checking if Sheet Exists? brett.kaplan@gmail.com Microsoft Excel Misc 5 1st Sep 2006 03:27 PM
Checking to see that a CHART exists prior to running a simple command Anton Microsoft Excel Misc 0 29th Aug 2006 06:30 AM
checking to see if DDL value exists Darrel Microsoft ASP .NET 4 10th Dec 2004 03:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:49 AM.