感谢您的帮助罗宾!我对您的代码的主要问题是用户仍然必须手动收缩轴以使线条看起来像是向无穷大延伸。
我最终将我的所有数据系列设置在相同的轴上,并将 X 和 Y 阈值定义为非常高的数字(例如 500000)。之后,我通过将数据集的最大数量乘以 1.1 或用户定义的限制乘以 1.1 来设置轴限制,以较大者为准。
您的解决方案可能代码更优雅,需要的资源更少,但在图表格式方面我是个怪人:D
Horz(1) = 0
Horz(2) = 500000
Vert(1) = 0
Vert(2) = 500000
'First Example Data Series
With ActiveChart.SeriesCollection.NewSeries
.Name = ActiveSheet.Cells(1, 2) & " Max Old"
.ChartType = xlXYScatterLines
.AxisGroup = xlPrimary
.XValues = "='Graph'!$AE$3:$AE$4"
.Values = Vert
.Select
.Format.Line.Weight = 2.25
.Format.Line.Visible = True
.Format.Line.ForeColor.RGB = RGB(195, 214, 155) 'Light Green
.Format.Line.DashStyle = msoLineDash
.MarkerStyle = -4142
End With
'Second Example Data Series
With ActiveChart.SeriesCollection.NewSeries
.Name = ActiveSheet.Cells(2, 2) & " Max Old"
.ChartType = xlXYScatterLines
.AxisGroup = xlPrimary
.XValues = Horz
.Values = "='Graph'!$AE$5:$AE$6"
.Select
.Format.Line.Weight = 2.25
.Format.Line.Visible = True
.Format.Line.ForeColor.RGB = RGB(217, 150, 148) 'Light Red
.Format.Line.DashStyle = msoLineDash
.MarkerStyle = -4142
End With
With ActiveChart
'Set the X axis limit
.Axes(xlCategory, xlPrimary).MinimumScale = 0
.Axes(xlCategory, xlPrimary).MaximumScale = WorksheetFunction.RoundUp(Application.Max(ActiveChart.SeriesCollection(1).XValues) * 1.1, 0)
'Set the Y axis limit
.Axes(xlValue, xlPrimary).MinimumScale = 0
If Application.Max(ActiveChart.SeriesCollection(1).Values) >= Application.Max(ActiveChart.SeriesCollection(5).Values) Then
.Axes(xlValue, xlPrimary).MaximumScale = WorksheetFunction.RoundUp(Application.Max(ActiveChart.SeriesCollection(1).Values) * 1.1, 0)
Else
.Axes(xlValue, xlPrimary).MaximumScale = WorksheetFunction.RoundUp(Application.Max(ActiveChart.SeriesCollection(5).Values) * 1.1, 0)
End If
End With
Graph