Delete chart from a specific cell using VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've used the code below to move a chart from one sheet to another (from Sheet 5 to cell M2 in Sheet 1).

However, when re-running the code to create the chart, I would like to delete the existing chart in cell M2 of Sheet 1, without deleting the other charts in Sheet 1.

Does anyone know how to do this, please?

TIA

Code:
Sub MoveChart()
Sheet5.Activate
ActiveSheet.ChartObjects(1).Cut
Sheet1.Activate
Range("M2").Select
ActiveSheet.Paste
End Sub
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You can name your chart after you've moved it, so that you can use that same name to delete it. For example, let's say that you want to name the chart "MyChart", first delcare a constant at the module level to hold the name of the chart, and then use 2 procedures, one to move the chart and the other to delete it, as follows...

Code:
Option Explicit

Const CHART_NAME As String = "MyChart"


Sub MoveChart()
    Sheet5.ChartObjects(1).Cut
    With Sheet1
        .Paste .Range("M2")
        .ChartObjects(.ChartObjects.Count).Name = CHART_NAME
    End With
End Sub


Sub DeleteChart()
    Sheet1.ChartObjects(CHART_NAME).Delete
End Sub

Otherwise, you can use the following code to delete the chart located in M2 of Sheet1...

Code:
    Dim chrtObj As ChartObject
    
    With Sheet1
        For Each chrtObj In .ChartObjects
            If Not Intersect(chrtObj.TopLeftCell, .Range("M2")) Is Nothing Then
                chrtObj.Delete
                Exit For
            End If
        Next chrtObj
    End With

Hope this helps!
 
Last edited:
Upvote 0
Actually, for the procedure called DeleteChart(), we should add error handling, just in case you run it when the chart does not exist...

Code:
Sub DeleteChart()    On Error Resume Next
    Sheet1.ChartObjects(CHART_NAME).Delete
    On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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