Excel 2010 中的 VBA 位置图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24782404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:53:02  来源:igfitidea点击:

VBA Position Chart in Excel 2010

vbacharts

提问by art123456

I am trying to add a chart to a worksheet and then position it according to a certain range of cells. I am using the .top, .left, etc. functions and it keeps giving me an error that my object does not support the method being used. What am I doing wrong?! Here is my code:

我正在尝试将图表添加到工作表,然后根据一定范围的单元格定位它。我正在使用 .top、.left 等函数,它不断给我一个错误,我的对象不支持正在使用的方法。我究竟做错了什么?!这是我的代码:

Sub AddCharts()

Range("O1").Select

Dim sh As Worksheet
Dim chrt As Chart
Dim lastrow As Long

lastrows = Range("A2").End(xlDown).Row

Set sh = ActiveWorkbook.Worksheets("TraceTable")
Set chrteit = sh.Shapes.AddChart.Chart

With chrteit
.ChartType = xlXYScatter
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = sh.Range(Cells(2, 6), Cells(lastrows, 6))
.SeriesCollection(1).Values = sh.Range(Cells(2, 7), Cells(lastrows, 7))

    .HasTitle = True
    .ChartTitle.Text = "EIT"
    .Height = Range("N2:N14").Height
    .Width = Range("N2:T2").Width
    .Top = Range("N2").Top
    .Left = Range("N2").Left
End With

End Sub

回答by djikay

To continue on from my comment below the original post, try replacing:

要继续我在原始帖子下方的评论,请尝试替换:

.Height = Range("N2:N14").Height
.Width = Range("N2:T2").Width
.Top = Range("N2").Top
.Left = Range("N2").Left

with:

和:

.Parent.Height = Range("N2:N14").Height
.Parent.Width = Range("N2:T2").Width
.Parent.Top = Range("N2").Top
.Parent.Left = Range("N2").Left

回答by David Zemens

A Chartdoesn't have the .Top, .Left, etc., properties, but it's parent container, the ChartObjectdoes have those properties.

一个Chart不具备.Top.Left等等,性质,但它的父容器中,ChartObject确实有这些属性。

So instead of .Height =do .Parent.Height =, etc.

所以,而不是.Height =do.Parent.Height =等。

.Parent.Height = Range("N2:N14").Height
.Parent.Width = Range("N2:T2").Width
.Parent.Top = Range("N2").Top
.Parent.Left = Range("N2").Left