Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel - check if chart exists / delete chart

Status
Not open for further replies.

JFF

Programmer
May 6, 2001
13
US
The code below creates a chart. I would like to put this code in a loop and then 1) have code before it that checks to see if this "Results" charts exists, and 2) if it does exist - to delete it. Please help.

MyRange = Range("B4:B204")
Range(MyRange).Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Info").Range(MyRange), _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Info"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
ActiveChart.HasLegend = False
'- Names the Chart.
ActiveChart.Parent.Name = "Results"

Thanks
John
 
Just use the macro recorder to record yourself deleting the chart. Insert that code at the beginning of what you already have.

Just before that code, put in:
[COLOR=blue white]on error resume next[/color]
and just after the recorded code:
[COLOR=blue white]on error goto 0[/color]


[tt]_____
[blue]-John[/blue]
[/tt][red]"If you're flammable and have legs, you are never blocking a fire exit."[/red]
-Mitch Hedberg

Help us help you. Please read FAQ181-2886 before posting.
 
This is what I would use.

Code:
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Charts.Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
 
I have other Charts in the sheet that I do want to keep. Will the ...

ActiveWorkbook.Charts.Delete

... delete all of them?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top