使用 VBA 中的非连续数据创建 XY 图表

Create a XY Chart with non-contiguous data in VBA

我想根据下面购物车中的非连续数据创建图表(请暂时忽略标签、图例位置和网格线):

    Sub CreateChart2()
'PURPOSE: Create a chart (chart dimensions are required)

Dim cht As ChartObject
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Tabelle4")
Dim rngx1 As Range
Dim rngx2 As Range
Dim rngy1 As Range
Dim rngy2 As Range


Set rngx1 = Application.InputBox(prompt:="Select cells to link" _
    , Title:="Select data label values", Default:=ActiveCell.Address, Type:=8)
Set rngy1 = Application.InputBox(prompt:="Select cells to link" _
    , Title:="Select data label values", Default:=ActiveCell.Address, Type:=8)  ' Series1 -> PlotOrder = 1
Set rngx2 = Application.InputBox(prompt:="Select cells to link" _
    , Title:="Select data label values", Default:=ActiveCell.Address, Type:=8)
Set rngy2 = Application.InputBox(prompt:="Select cells to link" _
    , Title:="Select data label values", Default:=ActiveCell.Address, Type:=8)  ' Series2 -> PlotOrder = 2
If Not rngx1 Is Nothing Then
    If Not rngy1 Is Nothing Then
        Set rng = Union(rngx1, rngy1)
    End If
End If
If Not rngx2 Is Nothing Then
    If Not rngy2 Is Nothing Then
        Set rng = Union(rngx2, rngy2)
    End If
End If
'Create a chart
    Set cht = ActiveSheet.ChartObjects.Add( _
      Left:=ActiveCell.Left, _
      Width:=300, _
      Top:=ActiveCell.Top, _
      Height:=200)
    With cht
        .SeriesCollection(1).Name = "=Tabelle4!$I"
        .SeriesCollection(1).XValues = rngx1
        .SeriesCollection(1).Values = rngy1
        .SeriesCollection(2).Name = "=Tabelle4!$I"
        .SeriesCollection(2).XValues = rngx2
        .SeriesCollection(2).Values = rngy2
        .SetElement (msoElementLegendBottom)
        .SetElement (msoElementPrimaryValueGridLinesNone)
        .SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
        .SetElement (msoElementPrimaryValueAxisTitleRotated)
        .SetElement (msoElementChartTitleCenteredOverlay)
    End With

'Determine the chart type
    cht.Chart.ChartType = xlXYScatter
      
End Sub

但是我在这条线上遇到了问题:

        With cht
        .SeriesCollection(1).Name = "=Tabelle4!$I

这条线有什么问题,你能帮我解决一下吗?

问题是创建图表后,您尝试配置系列,但它们不存在!您必须先创建它们。尝试替换这部分代码。

这里,图表创建好后,选中,类型改为XY Plot。

'Create a chart
Set cht = ActiveSheet.ChartObjects.Add( _
  Left:=ActiveCell.Left, _
  Width:=300, _
  Top:=ActiveCell.Top, _
  Height:=200)

' Select chart
cht.Activate

'Determine the chart type
cht.Chart.ChartType = xlXYScatter
    
With ActiveChart
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "=Tabelle4!$I"
    .SeriesCollection(1).XValues = rngx1
    .SeriesCollection(1).Values = rngy1

    .SeriesCollection.NewSeries
    .SeriesCollection(2).Name = "=Tabelle4!$I"
    .SeriesCollection(2).XValues = rngx2
    .SeriesCollection(2).Values = rngy2

    .SetElement (msoElementLegendBottom)
    .SetElement (msoElementPrimaryValueGridLinesNone)
    .SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    .SetElement (msoElementPrimaryValueAxisTitleRotated)
    .SetElement (msoElementChartTitleCenteredOverlay)
End With