Delete Chart If It Exists and Select Non-Contiguous Columns

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hello, I am trying to write a macro that builds a chart when it is run. However, if the chart already exists (ie if the macro has already been run at least once), I would to delete the existing chart and create a new one. To be honest the whole ChartObject vs Chart thing is not very clear to me, so I can't figure out how to delete my chart.
I'm trying to do something like
VBA Code:
On Error Resume Next
'somehow delete the chart
On Error GoTo 0
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If there is only one chart on the sheet, I think you can just write

ActiveSheet.ChartObjects(1).Delete
 
Upvote 0
If there is only one chart on the sheet, I think you can just write

ActiveSheet.ChartObjects(1).Delete
I have 3 charts. Should I do
VBA Code:
ActiveSheet.ChartObjects(1).Delete
ActiveSheet.ChartObjects(2).Delete
ActiveSheet.ChartObjects(3).Delete
?
 
Upvote 0
I have 3 charts. Should I do
VBA Code:
ActiveSheet.ChartObjects(1).Delete
ActiveSheet.ChartObjects(2).Delete
ActiveSheet.ChartObjects(3).Delete
?
Should be, but you can also loop through all charts like this example

 
Upvote 0
Solution
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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
Back
Top