在Excel VBA中,我需要绘制具有动态范围的散点图

时间:2013-12-09 14:29:01

标签: vba dynamic plot range scatter

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

在下面的代码中,如果开始行和结束行为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

1 个答案:

答案 0 :(得分:0)

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

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)