【问题标题】:In Excel VBA I need to plot a scatter plot with dynamic ranges在 Excel VBA 中,我需要绘制具有动态范围的散点图
【发布时间】:2013-12-09 14:29:01
【问题描述】:

我有从文本文件导入的测试结果表。这些表的行数和起始行不同。我已经完成了查找开始行和结束行的例程。

在下面的代码中,如果开始行和结束行是 12 和 211,则一切正常。如何将不同的索引提供给 setsourcedata 例程?我想在同一张图表上绘制 X 与 Y1 和 Y2。感谢您的帮助

With Charts.Add.Location(Where:=xlLocationAsObject, Name:=SheetName)

        .ChartType = xlXYScatterSmoothNoMarkers

        .SetSourceData Sheets("sheet1").Range("A12:A211,C12:C211,AD12:AD211"), xlColumns

        .HasTitle = True
        .ChartTitle.Font.Size = 10
        .ChartTitle.Text = "Test Date " & SheetName
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Passes"
        .Axes(xlCategory, xlPrimary).MinimumScale = 0
        .Axes(xlCategory, xlPrimary).MaximumScale = 10000
        .Axes(xlCategory, xlPrimary).HasMajorGridlines = True
        .Axes(xlCategory).Crosses = xlMinimum

        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Text = "Rut depth (mm)"
        .Axes(xlValue, xlPrimary).MinimumScale = -15
        .Axes(xlValue, xlPrimary).MaximumScale = 0
        .Axes(xlValue, xlPrimary).Crosses = xlMinimum
        'The Parent property is used to set properties of the Chart.

        With .Parent
              .Name = "MyDataChart"
              .Top = .Parent.Range("F2").Top
              .Left = .Parent.Range("E2").Left
              .Width = 350
              .Height = 200
        End With
  End With

【问题讨论】:

    标签: vba dynamic plot range scatter


    【解决方案1】:

    假设您的数据列是连续的(没有任何间歇性空白),您可以尝试以下操作:

    Dim col1 as Range, col2 as Range, col3 as Range
    
    Set col1 = ActiveSheet.Range("A12",Range("A12").End(xlDown))
    Set col2 = col1.Offset(0,2)  'Return column C, same # of rows 
    Set col3 = col1.Offset(0,3)  'Return column D, same # of rows 
    
    .SetSourceData Sheets("sheet1").Range(col1.Address & "," & col2.Address & "," & col3.Address)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      相关资源
      最近更新 更多