DESPERATELY NEED HELP: How to delete an old graph and replace with a new one [VBA Macro]

GKarl

New Member
Joined
Nov 8, 2014
Messages
1
Hi all,

I'm currently working on market research and data analysis for my company, which specializes in fashion products.

I am trying to generate two different macro buttons that generate graphs in a demarcated space for the below data, one graph for product type and one graph for style.

________________________
Product TypeQuantity SoldTotal RevenueTotal CostTotal Profit
Dresses878436.628465590.6
Tops585630.422103420.4
Pants8285053055.65449.4
Shorts575659.22102.43556.8
Style Analysis
StyleQuantity SoldTotal RevenueTotal CostTotal Profit
Glamorous797311.62754.64557
Natural697059.62220.84838.8
Dramatic595743.82220.63523.2
Street778116.230185098.2

<colgroup><col><col><col><col span="2"></colgroup><tbody>
</tbody>

________________________
ERROR BOLDED

Sub BrandGraph()
'
' BrandGraph Macro
'
If ActiveChart = "Chart1" Then
Charts("Chart1").Delete
Else


ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Range("'Profit Analysis'!$A$10:$E$15")
ActiveChart.SeriesCollection(1).Select
Selection.Delete
Charts.Add.Name = "Chart1"

End If




End Sub

So what the code does above, is basically, when I click the button for it, it is supposed to delete the old graph in that space, and generate a new one based on its data range, i.e. I click "Brand" and the old graph, whatever is in the demarcated space, deletes, and a new Brand analysis graph shows up, and I click "Style" and the old Brand graph deletes and the "Style" graph takes its place instead.

I have the code that generates a new graph - but I can't figure out the code to get the existing graph to delete itself. Can someone help me fix the problem, or hopefully help me come up with code to do all that above?

YOUR HELP IS GREATLY APPRECIATED. I will shower you with positive energy and beautiful praise.

Thank you very very much!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I wonder if this:

Code:
If ActiveChart = "Chart1" Then
    Charts("Chart1").Delete
Else

would work if you adjusted it to this:

Code:
If ActiveChart.Parent.Name = "Chart1" Then
  ActiveChart.Delete
Else

But using a chart object name (chart object is the parent shape of a chart) is error prone, because you never really know what name Excel gives to a new chart.

You could just assume that the relevant chart is selected when the macro is run, and if it isn't, remind the user to select a chart:

Code:
If ActiveChart Is Nothing Then
    MsgBox "Select the chart to delete, then try again."
Else
    ActiveChart.Parent.Delete
    ' code to replace/recreate the chart
End If
 
Upvote 0

Forum statistics

Threads
1,215,428
Messages
6,124,832
Members
449,190
Latest member
rscraig11

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