在excel中定义新图表的位置

时间:2016-06-08 13:06:07

标签: vba excel-vba excel

我正在添加一个新图表,并希望定义其位置。 我曾尝试过几种方法,包括顶部和左侧,但没有成功。 这是我的代码:

Set sh = ActiveWorkbook.Worksheets(unit)
Set chrt = sh.Shapes.AddChart.Chart

With chrt
    'Data?
    .ChartType = xlXYScatterLines
    .SeriesCollection.NewSeries
    .SeriesCollection(1).name = "=""Scatter Chart"""
    .SeriesCollection(1).XValues = x
    .SeriesCollection(1).Values = y


    'Location
    'DON'T KNOW WHAT TO PUT HERE
    '.location xlLocationAsObject doesn't work!
    'Titles
    .HasTitle = True
    .ChartTitle.Characters.Text = name
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X values"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y values"
    .Axes(xlCategory).HasMajorGridlines = True

    'Formatting
    .Axes(xlCategory).HasMinorGridlines = False
    .Axes(xlValue).HasMajorGridlines = True
    .Axes(xlValue).HasMinorGridlines = False
    .Axes(xlValue).MinimumScaleIsAuto = True
    .Axes(xlValue).MaximumScaleIsAuto = True
    .HasLegend = False
End With

1 个答案:

答案 0 :(得分:1)

您可以使用它来设置要将图表移动到的单元格。只需将其添加到循环外

sh.ChartObjects.Left = sh.Range("A1")
sh.ChartObjects.Top = sh.Range("A1")

希望这有帮助