VBA-excel paste chart as a picture

北城以北 提交于 2019-11-28 04:33:38

问题


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

回答1:


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



回答2:


Range("A1:A8").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$A$8")
ActiveChart.ChartType = xlLine
ActiveChart.PlotArea.Select
ActiveChart.ChartArea.Copy


Range("A20").Select
ActiveSheet.Paste



回答3:


A bit slow but lets you convert charts to pictures. I use this to create reports (in a prior step I copy charts from another tab, where the original charts are created)

ActiveSheet.ChartObjects.Select 
Selection.Copy                        'copy charts 
Range("A1").Select                    'choose destination
ActiveSheet.Pictures.Paste.Select     'paste as pictures
ActiveSheet.ChartObjects.Delete       'delete charts (leaving just pictures)


来源:https://stackoverflow.com/questions/13327420/vba-excel-paste-chart-as-a-picture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!