交换轴绘制VBA中的散点图

时间:2013-04-02 16:15:31

标签: excel excel-vba charts vba

使用下面的代码,我得到了我想要的图形,除了它以错误的方式绘制轴。如何将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

1 个答案:

答案 0 :(得分:3)

将数据添加到图表后,您需要单独设置XValuesValues

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")

其中一种方法可以帮到你。