PDA

View Full Version : Code To Insert Chart And Delete Charts Using VBA



jeff
08-09-2013, 10:02 PM
Please see attached sample workbook

There are 3 information sets which I require a chart for each set.

I want to be able to use code to see the 3 sets of data and then with button selection insert the charts ( like the chart position /stylein workbook attached)

There also is a button to delete the Charts

Best Regards

Jeff

Excel Fox
08-10-2013, 09:04 AM
Are you saying that you will have 3 sets of data, and on the click of a button, you want 3 charts to be prepared. The position of the chart will be based on where the data set is?

jeff
08-10-2013, 09:32 AM
Yes thank you
Jeff

Excel Fox
08-10-2013, 10:52 PM
Jeff, here's one code. I'm sure you can replicate this for all the other charts.



Sub AddChart()

With ActiveSheet.Shapes.AddChart
.Width = 544.5: .Height = 171.25: .Left = 291
With .Chart
.SetSourceData Source:=Range("Sheet1!$B$21:$E$23")
.ChartType = xlColumnClustered
.Legend.Delete
.Axes(xlValue).Delete
.Axes(xlValue).MajorGridlines.Delete
.SeriesCollection(1).ApplyDataLabels
End With
With ActiveSheet.ChartObjects(.Name).Border
.Color = 0
.LineStyle = 1
.Weight = 2.6
End With
End With

End Sub


And the code to delete all charts in the active sheet is
Activesheet.ChartObjects.Delete

jeff
08-11-2013, 04:15 AM
ExcelFox
Thanks for the code to create & delete charts.
Is it best to place the code using Insert Module or is it best to place in Sheet Module
As I only have 1 sheet I inserted into Module and it works
Regards
Jeff