VBA图表X轴具有重复值,并且不与数据对齐

时间:2018-07-18 14:35:44

标签: vba charts axis

我正在使用一些VBA代码来制作动态折线图。 X轴是月份,Y轴是该月内通过和失败的数量。到目前为止,我发现,如果我的数据有多个月(其中有10到12个月),则该图表似乎很好。但是,如果我只有两个月(请参见图片),则A轴将重复值,并且图形点无法正确对齐其应有的月份。有人遇到过类似的事情吗?

这是我正在使用的代码

Dim sh As Worksheet
Set sh = Worksheets("Summary")

With sh
    LR = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

Dim cht1 As CHART
Set cht1 = sh.Shapes.AddChart.CHART

For q = 1 To 2
    With cht1
        .Axes(xlValue).MinimumScale = 0
        .ChartType = xlXYScatterLines
        .SeriesCollection.NewSeries
        .SeriesCollection(q).Name = sh.Cells(2, q + 1)
        .SeriesCollection(q).XValues = sh.Range(sh.Cells(3, 1), sh.Cells(LR, 1))
        .SeriesCollection(q).Values = sh.Range(sh.Cells(3, q + 1), sh.Cells(LR, q + 1))
        .SeriesCollection(q).MarkerSize = 6
        .SeriesCollection(q).Border.ColorIndex = 11 - q
        .SeriesCollection(q).Trendlines.Add
        With .SeriesCollection(q).Trendlines(1)
            .Border.ColorIndex = 43 * (2 - q) + 3 * (q - 1)
            .Border.Weight = xlMedium
            .Border.LineStyle = xlDash
            .Border.LineStyle = xlContinuous
        End With
        .HasTitle = True
        .ChartTitle.Text = sh.Cells(1, 2)
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "DATE"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "AMOUNT"
    End With
Next

With cht1.Parent
    .Top = sh.Cells(LR + 2, 1).Top
    .Left = sh.Cells(LR + 2, 1).Left
    .Height = sh.Range(sh.Cells(LR + 2, 1), sh.Cells(LR + 20, 1)).Height
    .Width = sh.Range(sh.Cells(LR + 2, 1), sh.Cells(LR + 2, 8)).Width
    H = .Height
    W = .Width
End With

谢谢你, 马丁

0 个答案:

没有答案