我正在尝试使用VBA创建散点图。数据源应该是两个不相交的列。它甚至工作过一次。但是,在我使用另一个VBA代码填充一列表后,它崩溃了。试图google但无济于事。希望有人可以帮忙!!
Sub XY_Chart()
Dim r1, r2, rngDataSource As Range
Dim iDataRowsCt As Long
Dim iDataColsCt As Integer
Dim iSrsIx As Integer
Dim chtChart As Chart
Dim srsNew As Series
Set r1 = Application.ActiveSheet.Range("C2:C88111")
Set r2 = Application.ActiveSheet.Range("E2:E88111")
Set rngDataSource = Application.Union(r1, r2)
With rngDataSource
iDataRowsCt = .Rows.Count
iDataColsCt = .Columns.Count
End With
'' Create the chart
ActiveSheet.Unprotect
Set chtChart = ActiveSheet.ChartObjects.Add( _
Left:=ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left + _
ActiveWindow.Width / 4, _
Width:=ActiveWindow.Width / 2, _
Top:=ActiveSheet.Rows(ActiveWindow.ScrollRow).Top + _
ActiveWindow.Height / 4, _
Height:=ActiveWindow.Height / 2).Chart
With chtChart
.ChartType = xlXYScatterline
'' Remove any series created with the chart
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
For iSrsIx = 1 To iDataColsCt - 1 Step 2
'' Add each series
Set srsNew = .SeriesCollection.NewSeries
With srsNew
.Name = rngDataSource.Cells(1, iSrsIx + 1)
.Values = rngDataSource.Cells(2, iSrsIx + 1) _
.Resize(iDataRowsCt - 1, 1)
.XValues = rngDataSource.Cells(2, iSrsIx) _
.Resize(iDataRowsCt - 1, 1)
End With
Next
End With
End Sub
我还有另一段代码可以做同样的事情。
错误.Chart.PlotBy = xlRows
错误是运行时错误'-2147221504'(80040000) 自动化错误 无效的OLEVERB结构
Sub GRPH()
Dim lastRow As Long
Dim average, std As Double
Dim cht As Object
Dim r1, r2, r3 As Range
'Find the last Row with data in a Column
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Set r1 = Application.ActiveSheet.Range("C2:C88111")
Set r2 = Application.ActiveSheet.Range("E2:E88111")
Set r3 = Application.Union(r1, r2)
Set cht = ActiveSheet.ChartObjects.Add(Left:=300, Width:=300, Top:=10, Height:=300)
With cht
.Chart.SetSourceData Source:=r3
.Chart.PlotBy = xlRows
.ChartType = xlXYScatterLines
End With
End Sub
答案 0 :(得分:0)
将您的.ChartType
更改为xlXYScatterlines
。在这样的时候最好记录一个宏并从那里获得正确的图表类型,特别是当它在图表类型行上抛出错误时。 ;)