VBA-excel粘贴图表为图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13327420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:23:42  来源:igfitidea点击:

VBA-excel paste chart as a picture

excelvbachartspaste

提问by user1115535

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?

我正在从同一来源创建各种图表。我希望能够将每个图表用 vba 剪切粘贴为图片。有谁知道正确的代码?

I tried with this but it does not work:

我试过这个,但它不起作用:

Range("B21:C22").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Graphs'!$B:$C")
ActiveChart.ChartType = xl3DPie
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Copy
ActiveSheet.Pictures.Paste.Select

回答by Doug Glancy

I always find copying charts confusing, but this does what you want, I think, and doesn't use any Selects, which is always nice.

我总是觉得复制图表令人困惑,但我认为这可以满足您的需求,并且不使用 any Selects,这总是很好。

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:$C")
    .ChartType = xl3DPie
    .ChartArea.Copy
End With
ws.Range("A2").PasteSpecial xlPasteValues
cht.Parent.Delete
End Sub

回答by Jeremy Thompson

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


Range("A20").Select
ActiveSheet.Paste

回答by user11345588

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)