gpt4 book ai didi

vba - Excel VBA : Add a scatter chart to worksheet as object

转载 作者:行者123 更新时间:2023-12-02 01:28:18 25 4
gpt4 key购买 nike

我正在创建一个散点图,按下按钮后,会在 Sheet 1 中创建一个散点图。并使用 A2:A11作为 x值和 B2:B11作为 y值(value)观。

<罢工>

使用底部的代码允许我根据 A1:B3 中的值创建散点图。 (从 here 得到这个)。它很接近,但不完全是我正在寻找的。我如何调整它以满足我的需要?

我现在已经设置好了,所以根据我想要的值制作了图表,但我无法让它显示为 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

最佳答案

您编写的代码将图表添加为图表表,而不是工作表上的图表

试试这个:

替换

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

关于vba - Excel VBA : Add a scatter chart to worksheet as object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13794210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com