1

使用以下代码,我得到了我想要的图形,只是它以错误的方式绘制了轴。如何将 X 数据更改为 Y 数据,反之亦然?

    Sub AddChartObject()


    Set cht = ActiveChart
    Set rng = ActiveSheet.Range("AB30:AD40")



    With ActiveSheet.ChartObjects.Add(Left:=rng.Left, Width:=rng.Width, Top:=rng.Top, Height:=rng.Height)
     '(Left:=100, Width:=375, Top:=75, Height:=225)
        .Chart.ChartType = xlXYScatterLines

        .Chart.SetSourceData Source:=Range("O30:O40,N30:N40")


    End With


End Sub
4

1 回答 1

3

将数据添加到图表后,您需要单独设置XValues和。Values

Dim srs as Series

With ActiveSheet.ChartObjects.Add(Left:=rng.Left, Width:=rng.Width, Top:=rng.Top, Height:=rng.Height)
 '(Left:=100, Width:=375, Top:=75, Height:=225)
    .Chart.ChartType = xlXYScatterLines

    '.Chart.SetSourceData Source:=Range("O30:O40,N30:N40")
    Set srs = .Chart.SeriesCollection.NewSeries
    srs.Name = "The series name" '<-- edit as needed
    srs.XValues = Range("O30:O40")
    srs.Values = Range("N30:N40")

End With

如果那不这样做,那么只需翻转 Values/xValues,如:

    srs.XValues = Range("N30:N40")
    srs.Values = Range("O30:O40")

这些方法之一将为您做到这一点。

于 2013-04-02T16:23:27.467 回答