Question posted 2012 · +4 upvotes
I am creating various charts from the same source. I would like to be able to cut paste with vba each chart as a picture. Does anyone know the right code?
I tried with this but it does not work:
Range("B21:C22").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Graphs'!$B$21:$C$22")
ActiveChart.ChartType = xl3DPie
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Copy
ActiveSheet.Pictures.Paste.Select
Accepted answer +9 upvotes
I always find copying charts confusing, but this does what you want, I think, and doesn’t use any Selects, which is always nice.
Sub CreateAndCopyChart()
Dim ws As Worksheet
Dim cht As Chart
Set ws = ThisWorkbook.Worksheets("Graphs")
Set cht = ws.Shapes.AddChart.Chart
With cht
.SetSourceData ws.Range("$B$21:$C$22")
.ChartType = xl3DPie
.ChartArea.Copy
End With
ws.Range("A2").PasteSpecial xlPasteValues
cht.Parent.Delete
End Sub
VBA Core objects referenced (5)
ActiveSheet.Shapes— Working with shapes (drawing objects)ActiveSheet.Shapes— Events, Worksheet Functions, and ShapesAddChart.Select— Select a RangeAddChart.Select— Selecting and Activating CellsParent.Delete— SolverDelete Function
Top vba Q&A (6)
- Difference between Visual Basic 6.0 and VBA +122 (2009)
- VBA – how to conditionally skip a for loop iteration +116 (2011)
- VBA: Test if string begins with a string? +53 (2013)
- html parsing of cricinfo scorecards +47 (2012)
- Code to loop through all records in MS Access +46 (2011)
- Access VBA | How to replace parts of a string with another string +44 (2011)
vba solutions on this site
.