我试图将图表添加到工作表,然后根据一定范围的单元格放置它。我正在使用.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
Run Code Online (Sandbox Code Playgroud)
一个Chart不具备.Top,.Left等等,性质,但它的父容器中,ChartObject确实有这些属性。
所以,而不是.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
Run Code Online (Sandbox Code Playgroud)
要继续根据我在原始帖子下方的评论,请尝试替换:
.Height = Range("N2:N14").Height
.Width = Range("N2:T2").Width
.Top = Range("N2").Top
.Left = Range("N2").Left
Run Code Online (Sandbox Code Playgroud)
与:
.Parent.Height = Range("N2:N14").Height
.Parent.Width = Range("N2:T2").Width
.Parent.Top = Range("N2").Top
.Parent.Left = Range("N2").Left
Run Code Online (Sandbox Code Playgroud)