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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,524
Office Version
  1. 2016
Platform
  1. Windows
If there is only one chart on the sheet, I think you can just write

ActiveSheet.ChartObjects(1).Delete
 

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
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
?
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,524
Office Version
  1. 2016
Platform
  1. Windows
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

 
Solution

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows

Forum statistics

Threads
1,141,070
Messages
5,704,112
Members
421,327
Latest member
Msh

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