7

我正在寻找创建一个散点图,在按下按钮时,它会在其中创建一个散点图Sheet 1A2:A11用作x值和B2:B11y

A1:B3使用底部的代码允许我根据(从此处获取)中的值创建散点图。它很接近,但不完全是我想要的。我怎样才能调整它以满足我的需要?

我现在已经设置好了,所以图表是根据我想要的值制作的,但我不能让它在Sheet 1. 我该怎么做呢?.Location xlLocationAsObject似乎不起作用。

Private Sub chartButton_Click()
    ActiveWorkbook.Charts.Add
    With ActiveWorkbook.ActiveChart
        'Data?
        .ChartType = xlXYScatter
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = "=""Scatter Chart"""
        .SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$11"
        .SeriesCollection(1).Values = "=Sheet1!$B$2:$B$11"

        'Location
        'DON'T KNOW WHAT TO PUT HERE
        '.location xlLocationAsObject doesn't work!

        'Titles
        .HasTitle = True
        .ChartTitle.Characters.Text = "Scatter Chart"
        .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
        .HasLegend = False

    End With
End Sub
4

2 回答 2

8

您编写的代码将图表添加为 a Chart Sheet,而不是 a 上的图表Worksheet

试试这个:

代替

ActiveWorkbook.Charts.Add
With ActiveWorkbook.ActiveChart

Dim sh As Worksheet
Dim chrt As Chart

Set sh = ActiveWorkbook.Worksheets("Sheet1")
Set chrt = sh.Shapes.AddChart.Chart
With chrt

然后你可以控制它的位置和大小

    .ChartArea.Left
    .ChartArea.Top 
    .ChartArea.Height
    .ChartArea.Width 
于 2012-12-10T04:21:36.450 回答
0

使用时Chart.Location,需要指定位置:

.Location Where:=xlLocationAsObject, Name:="Sheet1"
于 2015-07-31T14:23:38.497 回答