Consulting

Results 1 to 18 of 18

Thread: Deleting a Chart in VBA

  1. #1

    Deleting a Chart in VBA

    Hi! This is my very first post here, so hello everyone!

    I am writing a marco that creates a chart everytime it is run, however, I want the previous chart to be deleted before the new one is created. Right now, each chart is just layered over one another. I tried recording a macro to see the code for chart deletion, but it references a chart name, such as Chart58. Because each time the macro is run a new chart name would be assigned, I can't use this kind of name. I have tried deleting rows as well. That works when I do it, but not when I run the code. Any ideas!
    Thanks!!!!

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    If the code creates a chart, name the chart within the code. You can Delete the chart the same way. If the chart were simply named "MyChart"

    It would be something like

    ActiveSheet.ChartObjects("MyChart").Delete

    The Chart58 incremental naming is the default.

    To start, rename the 1 remaining chart you have to the new name (make sure the code you have assigns that name to the name property of the new chart...)

    When you add the chart, specify the name

    ActiveSheet.ChartObjects.Add ("MyChart")

    Assigning a controlled name makes it easier.
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Can I just ask about the use of the phrases


    Quote Originally Posted by XLGibbs
    ActiveSheet.ChartObjects("MyChart").Delete
    and

    ActiveSheet.ChartObjects.Add ("MyChart")
    The name of the exixting chart is used before the ".delete" when deleteing yet when adding a chart the name of the object is used after the ".Add".


    Does it always have to be in this order?

    Ted
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    I think it's because the chartobject isn't already present so you can't refer to it (when adding). When you delete, you have to specify the object to delete.

    Charlize

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub RenewChart()

    Application.ScreenUpdating = False
    On Error Resume Next
    ActiveSheet.ChartObjects.Delete
    On Error GoTo 0
    Charts.Add
    With ActiveChart
    .ChartType = xlColumnClustered
    .SetSourceData Source:=Sheets("Sheet1").Range("G1:H20")
    .Location Where:=xlLocationAsObject, Name:="Sheet1"
    End With
    Application.ScreenUpdating = True

    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Thankyou.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Hi again! Thanks for all the replies. I tried all of the suggestions, including the exact code from mdmackillop and it still won't delete the chart. Any clue on why this isn't working? Or if it's even possible? Thanks again!!!!!!

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a workbook sample? Use Manage Attachments in the Go Advanced section.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Ok, I attached a portion of the code that I working with. Mine is much more detailed as far as the chart goes, but I guess you don't need that to see what I am doing wrong. Thanks so much for you help!

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try stepping though this. Does it select anything?
    [VBA] ActiveSheet.ChartObjects.Select
    Selection.Delete
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Ok, found out a couple of things:
    1) It doesn't seem like it is selecting anything.
    2) If I run the macro from the button on another sheet, it will run without errors, but will not actually delete the chart.
    3) If I run the macro on the actual sheet holding the chart by selected Tools/Macro/Run Macro, it gives me this error "Object doesn't support this property or method." and highlights the "Selection.Delete" line.

    I am so confused. I just can't figure out why this won't work! Again, any ideas are VERY appreciated. Thanks for all your help so far!

  12. #12
    I also found out if I use this:
    ActiveSheet.ChartObjects.Select
    ActiveSheet.ChartObjects.Delete

    And I press the button, it doesnt work but runs. If I select this macro while on the sheet with the chart, it does.

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post the actual workbook, removing any sensitive data.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Quick question - why are you deleting the chart anyway? Doesn't it automatically refresh when your data changes?

  15. #15
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Here's some example code for adding and then deleting a chart. HTH. Dave
    [VBA]
    Sub TempChart()
    Dim ChartRange As Range
    Dim Xvalue As Range, Yvalue As Range
    'make chart (Sheet1 A1:B10 data)
    Set Xvalue = Sheets("Sheet1").Cells(1, 1)
    Set Yvalue = Sheets("Sheet1").Cells(10, 2)
    Set ChartRange = Sheets("Sheet1").Range(Xvalue, Yvalue)
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    ActiveChart.SetSourceData Source:=ChartRange, PlotBy:=xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
    MsgBox "The chart is about to delete"
    'Delete chart
    ActiveChart.ChartArea.Select
    ActiveWindow.Visible = False
    Selection.Delete
    End Sub
    [/VBA]

  16. #16
    Here is the workbook. A lot of the material is sensitive, so this won't make a lot of sense now. But the general idea is still there. Button is on the "Start" sheet.

    As far as why I need to delete it, it's because the way I recorded the chart macro (because I couldn't easily write the code for the style of lines, ranges, etc) it generates a new chart each time.

    Thanks again!

  17. #17
    Hey everyone! Got it to work! Just used the following:
    On Error Resume Next
    Sheets("Sheet8").Select
    ActiveSheet.ChartObjects.Select
    ActiveSheet.ChartObjects.Delete
    On Error GoTo 0

    I think the only thing I needed to do that I wasn't doing before was actually select the correct sheet. Thanks for your help!

  18. #18
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    I'm glad it's working for you - make sure you mark the thread as "Solved" using the thread tools at the top of the window.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •