使用图表格式化

我有一张表,我从表中生成图表。

我想生成2个图表。 一个具有绝对数字的图表和其他与百分比相同的数据的图表。

现在,为此,我正在使用两个代码,只需添加一行用Y.axis以百分比forms生成图表即可。

我想定义我的图表开始的列(例如:G7中的chart1)和G15中的chart2。 (我没有在我的代码中)

我也想定义我的图表的长度,高度和宽度(我没有这个在我的代码中)

如果你能帮我添加这个需求,并且在一个程序中完成,那将是非常好的。

Sub chartstatus() Dim rng As Range Dim cht As Object Set rng = ActiveSheet.Range("A2:E53") Set sh = ActiveSheet.Shapes.AddChart sh.Select Set cht = ActiveChart With cht .SetSourceData Source:=rng .ChartType = xlColumnClustered cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%" End With cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 255) '<~~ Red cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) cht.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) cht.HasTitle = True cht.ChartTitle.Text = "Result 2017" End Sub 

我使用相同的代码,删除行来生成第二个图表

 cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%" 

使用ChartObject创build和定义图表,然后修改其所有属性(如位置和维度)的更简单的方法。

下面的代码将创build第一个图表,将其放置在“G7”单元格中,然后修改它的尺寸以显示您需要修改的属性。

您可以为第二个图表添加另一个(使用简单的复制>>粘贴)。

 Option Explicit Sub chartstatus() Dim Rng As Range Dim ChtObj As ChartObject Set rng = ActiveSheet.Range("A2:E53") ' use ChartObject instead of shape Set ChtObj = ActiveSheet.ChartObjects.Add(100, 100, 500, 500) '<-- default dimension and location >> can modify later With ChtObj .Chart.ChartType = xlColumnClustered .Chart.SetSourceData Rng With .Chart .Axes(xlSecondary).TickLabels.NumberFormat = "0.0%" .SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 255) '<~~ Red .SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) .SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) .HasTitle = True .ChartTitle.Text = "Result 2017" End With ' set position of the chart to Cell G7 .Top = Range("G7").Top .Left = Range("G7").Left ' change the dimensions of the chart With .Chart.ChartArea .Width = 1060 .Height = 420 End With End With End Sub 

要更改图表位置:

vba在Excel中的特定单元格位置添加形状

对于图表大小:

sh.Width = 100 sh.Height = 100