Finding appropriate sheets

prima satria

Board Regular
Joined
Nov 3, 2007
Messages
63
I made a button with vba code to run / produce a graph as a new sheet name "CHART X"

Sub CHART_SHEET()

Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("ITEM CUMMU.").Range("A8:D11"), _
PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Name = "='ITEM CUMMU.'!R7C2"
ActiveChart.SeriesCollection(2).Name = "='ITEM CUMMU.'!R7C3"
ActiveChart.SeriesCollection(3).Name = "='ITEM CUMMU.'!R7C4"
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="CHART X"
End Sub

it works, but the problem is when I run the button twice, the debug statement appears, and when click end button, it will produce a new sheet with the same chart.

What I want with the "chart" button is, when the sheets name "CHART X" already available the code won't run a new sheet, but can only go to the sheets "CHART X".

Is there anyone could help ? thx in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try adding this at the beginning of your code:

Code:
Application.DisplayAlerts = False
On Error Resume Next
Sheets("CHART X").Delete
On Error Go To 0
Application.DisplayAlerts = True
 
Upvote 0

prima satria

Board Regular
Joined
Nov 3, 2007
Messages
63
Mr. Andrew,

As instructed, I added the code and when I ran the button, the syntax

On Error Go To 0

was highlighted in red. Then I tried to write it off, and ran again the button. And it worked !

Thx so much, may God bless you.
 
Upvote 0

Forum statistics

Threads
1,190,635
Messages
5,982,050
Members
439,751
Latest member
sohamkhatri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top