Chart Object As It's Own Sheet - How do you delete via VBA?

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
69
I have a workbook with one data sheet and one chart as another sheet. I'm trying to write a macro that would delete the chart - however, Excel doesn't recognize chart as a sheet. The macro recorder lists the chart as a sheet.

How do I go about deleting the chart via VBA (I'm Excel 2007)?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
try
Code:
[FONT=Courier New]Sub AllChartsDelete()[/FONT]
[FONT=Courier New]On Error GoTo ExitChart[/FONT]
[FONT=Courier New]Dim ws As Worksheet[/FONT]
[FONT=Courier New]Dim chObj As ChartObject[/FONT]
[FONT=Courier New]Application.DisplayAlerts = False[/FONT]
[FONT=Courier New]'delete embedded charts[/FONT]
[FONT=Courier New]For Each ws In ThisWorkbook.Worksheets[/FONT]
[FONT=Courier New]For Each chObj In ws.ChartObjects[/FONT]
[FONT=Courier New]chObj.Delete[/FONT]
[FONT=Courier New]Next chObj[/FONT]
[FONT=Courier New]Next ws[/FONT]
[FONT=Courier New]'delete the chart sheets[/FONT]
[FONT=Courier New]ThisWorkbook.Charts.Delete[/FONT]
[FONT=Courier New]ExitChart:[/FONT]
[FONT=Courier New]Application.DisplayAlerts = True[/FONT]
[FONT=Courier New]Exit Sub[/FONT]
[FONT=Courier New]End Sub[/FONT]
 
Upvote 0
Thanks for the reply.

That's the problem that I've ran into - I've used similar logic to defeat worksheets, but VBA doesn't recognize the chart as a worksheet, so it doesn't process it.

I've used this logic:

Sub SeeSheetNames()

Dim iSheetCount As Integer
Dim iSheet As Integer

iSheetCount = ActiveWorkbook.Sheets.Count
For iSheet = 1 To iSheetCount
Worksheets(iSheet).Activate
MsgBox Worksheets(iSheet).Name
Next iSheet

Using this method, the number of sheets is correct (2), and the name of the first worksheet shows up in the msgbox, but on the 2nd loop, I get a subscript out of range error -which indicates to me that Excel doesn't recognize the chart as a worksheet.

Any further thoughts?
 
Upvote 0
Thank you.

What would you define "cht" as? I tried ChartObject, but then I get a type mismatch.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,051
Members
449,206
Latest member
Healthydogs

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