问题描述
我有一张表,我正在从表中生成图表。我想生成2个图表。一个图表与绝对数字和其他图表相同的数据与百分比。
为此,我正在使用两个代码,只需添加一行用百分比形式生成图表。
我想定义列表,我的图表将从哪里开始(例如:G7的chart1)和G15的chart2。 (我的代码中没有这个)
我也想定义图表的长度,高度和宽度(我没有这个在我的代码中)
如果您可以帮助我添加此要求并在单个程序中执行,那将是非常好的。
Sub chartstatus()
Dim rng As Range
Dim cht As Object
设置rng = ActiveSheet.Range(A2:E53)
设置sh = ActiveSheet.Shapes.AddChart
sh.Select
设置cht = ActiveChart
带有cht
.SetSourceData来源:= 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
创建和定义图表,然后修改所有的属性(如位置和维度)。
下面的代码将创建第一张图表,将其放在单元格G7中,并修改其尺寸以显示您需要修改的属性。
您可以为第二张图表添加另一张图表(轻松复制>>粘贴)。
代码 / p>
Option Explicit
Sub chartstatus()
Dim Rng As Range
Dim ChtObj As ChartObject
设置rng = ActiveSheet.Range(A2:E53)
'使用ChartObject而不是形状
设置ChtObj = ActiveSheet .ChartObjects.Add(100,100,500,500)'< - 默认维度和位置>>可以稍后修改
使用ChtObj
.Chart.ChartType = xlColumnClustered
.Chart.SetSourceData Rng
带有.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
'图表的设置位置为单元格G7
.Top =范围(G7 ).Top
.Left = Range(G7)。左
'更改图表的尺寸
带.Chart.ChartArea
.Width = 1060
.Height = 420
结束
结束
结束子
I have a sheet and I am generating the Chart from a table.
I would like to generate 2 charts. One chart with absolute numbers and other chart for the same data with Percentage.
Right now, for this, I am using two code, just by adding a line for generating the chart with Y.axis in percentage.
I would like to define column where my chart will start (for eg: chart1 from G7) and chart2 from G15. (I don't have this in my code)
I also, would like to define the length , height and width for my chart.(I don't have this in my code)
It would be great if you can help me add this requirement and do it In a single program.
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
I use the same code, deleting the line to generate the second chart
cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
The easier way if to use the ChartObject
to create and define the chart, and then modify all it's properties (such as position and dimension).
The code below will create the first chart, place it in Cell "G7", and I modifed it's dimensions to show you the properties you need to modify.
You can add another one for the second chart (with an easy copy>>paste).
Code
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
这篇关于使用图表格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!