在图表中添加水平线

时间:2017-07-29 18:57:26

标签: excel vba excel-vba

我正在用表格生成图表。

我想在图表中有一条水平线。我把它称为设定点,数据在我表的E列中可用。

另外,我想改变m轴刻度。我希望轴值为0 - 10,百分号。

任何领导都会有所帮助。我用我的图表试过这个。

Sub chartRedResultPer()
Dim Rng, rng1, rng2  As Range
Dim cht1 As Object
Set rng1 = ActiveSheet.Range("A2:A53")
Set rng2 = ActiveSheet.Range("D2:D53")
Set Rng = Union(rng1, rng2)
Set Sh = ActiveSheet.ChartObjects.Add(Left:=320, _
    Width:=600, _
    Top:=70, _
    Height:=250)
Sh.Select
Set cht1 = ActiveChart
With cht1
.SetSourceData Source:=Rng
.ChartType = xlColumnClustered
cht1.Axes(xlSecondary).TickLabels.NumberFormat = "0%"
cht1.SeriesCollection(1).delete
End With
cht1.SeriesCollection(1).Name = "Red "
cht1.SeriesCollection(1).HasDataLabels = True
cht1.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
cht1.HasTitle = True
cht1.ChartTitle.Text = "Result 2017"
End Sub

2 个答案:

答案 0 :(得分:2)

添加另一个范围

Dim rng3 as Range
Set rng3 = ActiveSheet.Range("E2:E53")

With cht1.SeriesCollection.NewSeries
    .Name = "SetPoint"
    .Values = rng3
    .Type = xlLine
    .MarkerStyle = None
    .Format.Line.ForeColor.RGB = RGB(0, 0, 0)
End With

With cht1.Axes(xlValue)
        .MaximumScale = 10
        .MinimumScale = 0
End With

答案 1 :(得分:1)

< p>代码就是这样。< / p> < pre>< code> Sub ColumnChart()     Dim i As Integer,j As Integer,n As Integer     Dim rng1 As Range,rng2 As Range,rng3 As Range     Dim cht1 As Chart,Sh As ChartObject     设置rng1 = ActiveSheet.Range(" A2:A53")     设置rng2 = ActiveSheet.Range(" D2:D53")     设置rng3 = ActiveSheet.Range(" e2:e53")     设置Sh = ActiveSheet.ChartObjects.Add(左:= 320,_         宽度:= 600,_         上:= 70,_         高度:= 250)     设置cht1 = Sh.Chart     用cht1         n = .SeriesCollection.Count         如果n> 0然后             对于j = n到1步-1                 .SeriesCollection(J).Delete             下一个j         万一         .HasTitle =真         .ChartType = xlColumnClustered         .Legend.Position = xlLegendPositionRight         使用.ChartTitle             .Characters.Text ="结果2017"             .Characters.Font.Size = 12         结束             i = i + 1             .SeriesCollection.NewSeries             使用.SeriesCollection(i)                 .Name ="红色"                 .XValues = rng1                 .Values = rng2                 .Format.Fill.ForeColor.RGB = RGB(255,0,0)                 .ApplyDataLabels             结束             使用.SeriesCollection(i).DataLabels                 .ShowValue = True                 .Font.Size = 7             结束             i = i + 1             .SeriesCollection.NewSeries             使用.SeriesCollection(i)                 .Name =" Pecent"                 .XValues = rng1                 .Values = rng3                 .AxisGroup = 2                 .ChartType = xlLine                 .Format.Line.ForeColor.RGB = RGB(0,255,0)             结束             .Axes(xlValue,xlPrimary).HasTitle = True             .Axes(xlValue,xlPrimary).AxisTitle.Characters.Text =" Red"             .Axes(xlValue,xlPrimary).TickLabels.NumberFormat ="#,###"             .Axes(xlValue,xlSecondary).TickLabels.NumberFormat =" 0%"             .Axes(xlCategory,xlPrimary).HasTitle = True             .Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text =" x Axes"     结束 结束子 < /代码>< /预>