PC Review


Reply
Thread Tools Rate Thread

Chart.Name lives forever?

 
 
cate
Guest
Posts: n/a
 
      4th Apr 2010
At the beginning of the code I blow away any chart objects I find with

Dim co As ChartObject
For Each co In mySheet.ChartObjects
co.Delete
Next

And for good measure (this is probably all you have to do)

mySheet.ChartObjects.Delete

Afterwards, Chart.ChartObjects.Count is == 0

Later, I review the charts on the sheet with

For Each co In ws.ChartObjects
sTitle = co.Chart.Name
MsgBox ("has the name " & sTitle)
Next

Only one chart is found, but it's name is incrementing. This leads me
to believe that I really didn't blow away everything and that I can't
be sure I'm starting with a clean slate.

If I delete the sheet and recreate it, the chart names reset.

Why does this name persist?
 
Reply With Quote
 
 
 
 
Barb Reinhardt
Guest
Posts: n/a
 
      4th Apr 2010
At first blush, I wonder if you really do "blow them away". Add this to
your code

Try this

dim myWS as excel.worksheet
Dim co as Excel.chartojbect
set myWS = ActiveSheet

debug.print "Before: " & myWS.ChartObjects.Count

for each co in myWS.Chartobjects
co.delete
next co

debug.print "After: " & myWS.Chartobjects.Count

HTH,

Barb Reinhardt



"cate" wrote:

> At the beginning of the code I blow away any chart objects I find with
>
> Dim co As ChartObject
> For Each co In mySheet.ChartObjects
> co.Delete
> Next
>
> And for good measure (this is probably all you have to do)
>
> mySheet.ChartObjects.Delete
>
> Afterwards, Chart.ChartObjects.Count is == 0
>
> Later, I review the charts on the sheet with
>
> For Each co In ws.ChartObjects
> sTitle = co.Chart.Name
> MsgBox ("has the name " & sTitle)
> Next
>
> Only one chart is found, but it's name is incrementing. This leads me
> to believe that I really didn't blow away everything and that I can't
> be sure I'm starting with a clean slate.
>
> If I delete the sheet and recreate it, the chart names reset.
>
> Why does this name persist?
> .
>

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      5th Apr 2010
Hi cate,

The numeric suffix of the chart names continues to increment until you close
the workbook and re-open it. If you create 3 charts in a freshly opened
workbook then their names will be Chart 1, Chart 2 and chart 3.

If you then delete them all and create another one before closing the
workbook, then it will be Chart 4.

I am suspecting that your question relates to being able to reference the
charts after they are created and if so, you need to assign names to the
charts at the time of creating.

The following code example creates a charts and names it. If you are
creating several charts then you can use a loop and concatenate "Chart" with
the loop variable for the chart name or you might even want to give them a
more meaningful name.


Sub CreateCharts()
Dim rngChrtPos As Range
Dim ws As Worksheet
Dim chrtObj As ChartObject

Set ws = Sheets("Sheet1")
Set rngChrtPos = ws.Range("D2:G12")

With rngChrtPos
Set chrtObj = ws.ChartObjects.Add _
(.Left, .Top, .Width, .Height)

chrtObj.Name = "Chart 1"

End With

With chrtObj.Chart
.SetSourceData ws.Range("A1:B14")
.ChartType = xlLineMarkers
End With

End Sub

--
Regards,

OssieMac


 
Reply With Quote
 
cate
Guest
Posts: n/a
 
      5th Apr 2010
On Apr 4, 9:59*pm, OssieMac <Ossie...@discussions.microsoft.com>
wrote:
> Hi cate,
>
> The numeric suffix of the chart names continues to increment until you close
> the workbook and re-open it. If you create 3 charts in a freshly opened
> workbook then their names will be Chart 1, Chart 2 and chart 3.
>
> If you then delete them all and create another one before closing the
> workbook, then it will be Chart 4.
>
> I am suspecting that your question relates to being able to reference the
> charts after they are created and if so, you need to assign names to the
> charts at the time of creating.
>
> The following code example creates a charts and names it. If you are
> creating several charts then you can use a loop and concatenate "Chart" with
> the loop variable for the chart name or you might even want to give them a
> more meaningful name.
>
> Sub CreateCharts()
> Dim rngChrtPos As Range
> Dim ws As Worksheet
> Dim chrtObj As ChartObject
>
> Set ws = Sheets("Sheet1")
> Set rngChrtPos = ws.Range("D2:G12")
>
> With rngChrtPos
> * Set chrtObj = ws.ChartObjects.Add _
> * * * (.Left, .Top, .Width, .Height)
>
> * chrtObj.Name = "Chart 1"
>
> End With
>
> With chrtObj.Chart
> * .SetSourceData ws.Range("A1:B14")
> * .ChartType = xlLineMarkers
> End With
>
> End Sub
>
> --
> Regards,
>
> OssieMac


thank you. And you suspect correctly.
 
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
Chart.Name lives forever? cate Microsoft Excel Programming 3 5th Apr 2010 01:00 PM
unable to install Noone Lives Forever I =?Utf-8?B?YmlnbWFtbWE=?= Windows XP Games 1 23rd Nov 2004 12:37 AM
No one lives forever =?Utf-8?B?amF5?= Windows XP Games 1 17th Nov 2004 02:01 AM
no lives forever 2 demo internet =?Utf-8?B?ZGFu?= Windows XP Games 1 26th Mar 2004 11:43 AM
No One Lives Forever 2 T1000 ATI Video Cards 2 7th Sep 2003 01:06 PM


Features
 

Advertising
 

Newsgroups
 


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