ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Checking if a Chart Exists on a Sheet (https://www.excelbanter.com/excel-programming/386518-checking-if-chart-exists-sheet.html)

Keith Wilby

Checking if a Chart Exists on a Sheet
 
Newbie question:

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

Many thanks.

Keith.



Keith Wilby

Checking if a Chart Exists on a Sheet
 
"Keith Wilby" wrote in message
...
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.



[email protected]

Checking if a Chart Exists on a Sheet
 
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" wrote:
"Keith Wilby" wrote in message

...

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.




Peter T

Checking if a Chart Exists on a Sheet
 
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" wrote in message
...
"Keith Wilby" wrote in message
...
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.





[email protected]

Checking if a Chart Exists on a Sheet
 
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, 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" wrote:



"Keith Wilby" wrote in message


...


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 -




Keith Wilby

Checking if a Chart Exists on a Sheet
 
wrote in message
oups.com...

Many thanks Paul (and Peter).

Regards,
Keith.



Dave Peterson

Checking if a Chart Exists on a Sheet
 
One mo

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


All times are GMT +1. The time now is 07:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com