我注意到,当我使用range命令时,vba会忽略选择顺序。在我的代码中,当我在X轴上绘制A而在Y轴上绘制B时,效果很好。但是我不知道如何告诉它反转坐标。即使当我使用rng1 = Sheets("Sheet1").Range("B:B,A:A")
时,它仍然按先前的顺序排列系列。我的数据在表格中,我担心要保留绘图命令,因为我已经使用过了。
有人可以帮助我吗?
Set rng1 = Sheets("Sheet1").Range("A:A,B:B")
With ws
.Shapes.AddChart
Set objChrt = .ChartObjects(.ChartObjects.Count)
Set chrt = objChrt.Chart
With chrt
.ChartType = xlXYScatterSmoothNoMarkers
.SetSourceData Source:=rng1
End With
End With
答案 0 :(得分:1)
您可以分别为X和Y轴分配值。
假设您要绘制2个系列,这应该可行:
Sub create_chart()
Dim ws As Worksheet
Dim rngX1 As Range
Dim rngY1 As Range
Dim rngX2 As Range
Dim rngY2 As Range
Set ws = Worksheets("Sheets1")
Set rngX1 = Worksheets("Sheets1").Range("A:A")
Set rngY1 = Worksheets("Sheets1").Range("B:B")
Set rngX2 = Worksheets("Sheets1").Range("C:C")
Set rngY2 = Worksheets("Sheets1").Range("D:D")
With ws
.Shapes.AddChart
Set objChrt = .ChartObjects(.ChartObjects.Count)
Set chrt = objChrt.Chart
With chrt
.ChartType = xlXYScatterSmoothNoMarkers
'series 1
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = rngX1
.SeriesCollection(1).Values = rngY1
'series 2
.SeriesCollection.NewSeries
.SeriesCollection(2).XValues = rngX2
.SeriesCollection(2).Values = rngY2
End With
End With
End Sub
由于范围太小,您应该限制它以适合您的数据。