【问题标题】:VBA create xy scatter plot using multiple data seriesVBA使用多个数据系列创建xy散点图
【发布时间】:2020-08-20 04:34:06
【问题描述】:

我正在尝试使用 VBA 在其自己的工作表上创建 xy 散点图。我有两组要使用的数据。当我第一次运行下面显示的代码时(即没有“Power Chart”时),它会正确绘制数据。但是,当我重新运行带有“Power Chart”的代码时,我会显示另外 3 个系列,其中两个带有空白数据,一个只有 y 值,对应于我的工作表中的最后一列。然后,当我第三次运行代码时,我再次得到正确的图。继续重新运行我的代码会重复好情节 -> 坏情节 -> 好情节的循环。关于造成这种情况的任何想法?

Sub CreatingChartOnChartSheet()

Dim ch As Chart

Dim xrng As Range
Dim yrng1 As Range
Dim yrng2 As Range

Set ch = Charts.Add
Set xrng = Sheets("Power").Range("A2:A65536")
Set yrng1 = Sheets("Power").Range("D2:D65536")
Set yrng2 = Sheets("Power").Range("E2:E65536")

With ch

    ' If there is a previous chart, delete it
    For Each Chart In ActiveWorkbook.Charts
    If Chart.Name = "Power Chart" Then
        Application.DisplayAlerts = False
        Charts("Power Chart").Delete
        Application.DisplayAlerts = True
    End If
    Next Chart        
    
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "=""Series 1"""
    .SeriesCollection(1).XValues = xrng    
    .SeriesCollection(1).Values = yrng1
    
    .SeriesCollection.NewSeries
    .SeriesCollection(2).Name = "=""Series 2"""
    .SeriesCollection(2).XValues = xrng
    .SeriesCollection(2).Values = yrng2
    
    .SetElement (msoElementChartTitleAboveChart)
    .Name = "Power Chart"
    .ChartTitle.Text = "Power"
    .SetElement (msoElementLegendRight)
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Time (h)"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Text = "Power (kW)"
    '.Axes(xlCategory).MajorUnit = 1
    '.Axes(xlCategory).MinorUnit = 1
    
End With

End Sub

【问题讨论】:

    标签: excel vba plot scatter


    【解决方案1】:

    如果您在工作表上选择了数据时插入图表工作表或图表对象,图表将自动绘制所选数据。每当通过 VBA 添加图表时,最好先删除所有自动添加的系列。

    Sub CreatingChartOnChartSheet()
    
        Dim xrng As Range
        Dim yrng1 As Range
        Dim yrng2 As Range
        
        With Sheets("Power")
            Set xrng = .Range("A2:A65536")
            Set yrng1 = .Range("D2:D65536")
            Set yrng2 = .Range("E2:E65536")
        End With
    
        Application.DisplayAlerts = False
        On Error Resume Next
        Charts("Power Chart").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
        
        With Charts.Add
            'remove any auto-plotted data
            Do While .SeriesCollection.Count > 0
                .SeriesCollection(1).Delete
            Loop
            
            With .SeriesCollection.NewSeries
                .Name = "Series 1"
                .XValues = xrng
                .Values = yrng1
            End With
            
            With .SeriesCollection.NewSeries
                .Name = "=""Series 2"""
                .XValues = xrng
                .Values = yrng2
            End With
            
            .SetElement msoElementChartTitleAboveChart
            .Name = "Power Chart"
            .ChartTitle.Text = "Power"
            .SetElement (msoElementLegendRight)
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Time (h)"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Text = "Power (kW)"
            '.Axes(xlCategory).MajorUnit = 1
            '.Axes(xlCategory).MinorUnit = 1
            
        End With
    
    End Sub
    

    【讨论】:

    • 谢谢!只是为了让我理解,您设置的 Do While 循环将删除每个 SeriesCollection 直到没有剩余(即 .SeriesCollection.Count = 0)?另外,如果我去掉 .SeriesCollection(1).Delete 中的 (1),为什么这不起作用?
    • 另外,我看到您对代码进行了一些其他更改。这些是功能性的还是只是为了更好地组织事情(即使用 With Charts.Add 和 With.SeriesCollection.NewSeries)?
    • 是的 - 只要有第一个系列,它就会删除第一个系列。 SeriesCollection(1) 是特定系列,您可以将其删除。 SeriesCollection 不可删除。
    • 重新编辑。我喜欢以某种风格写作,所以我无法抗拒随时进行编辑 - 随意忽略任何/所有...使用With .SeriesCollection.NewSeries 不太容易出现维护问题,因为您不必担心如何许多系列已经存在。如果它不会导致行变得太混乱/太长,也倾向于删除“一次性”变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多