我在该位置使用宏生成图形。但是,由于我生成了多个图形,因此它们相互堆叠。我无法将它们正确地放在一起。我该怎么做?以下是我的代码:
Range("A318:A322,B318:B322").Select
'In the active sheet we add a chart
ActiveSheet.Shapes.AddChart.Select
' We set the source data for the chart
ActiveChart.SetSourceData Source:=Range( _
"'Assessment'!$A$318:$A$322,'Assessment'!$B$318:$B$322")
'We define the type of chart
ActiveChart.ChartType = xlColumnClustered
' Before we can perform an action on the chart we need to activate it
ActiveSheet.ChartObjects(1).Activate
'We perform the cut operation
ActiveSheet.ChartObjects(1).Cut
'we select the Sheet2 where we wish to paste our chart
'Sheets("Sheet2").Select
'We now paste the chart in the Sheet2 whic has become the active sheet after selection
'ActiveSheet.Paste
'we return to sheet1
Sheets("User Report").Select
ActiveSheet.Paste
' we select the cell F9 in sheet1
Range("D1").Activate
现在,我如何将图表粘贴到表格中所需的单元格中?
答案 0 :(得分:2)
不需要选择或激活:
Dim co
Set co = ActiveSheet.Shapes.AddChart()
co.Chart.SetSourceData Source:=Range("'Assessment'!$A$318:$A$322,'Assessment'!$B$318:$B$322")
co.Chart.ChartType = xlColumnClustered
co.Cut
With Sheets("Sheet2")
.Paste
Set co = .Shapes(.Shapes.Count)
co.Left = .Range("D1").Left
co.Top = .Range("D1").Top
End With
或者,将图表放在右侧工作表的正确位置开始。
Dim co As ChartObject
Set co = Worksheets("Sheet2").Shapes.AddChart(Left:=Worksheets("Sheet2").Range("D2").Left, _
Top:=Worksheets("Sheet2").Range("D2").Top)
co.Chart.SetSourceData Source:=Range("'Assessment'!$A$318:$A$322,'Assessment'!$B$318:$B$322")
co.Chart.ChartType = xlColumnClustered