我在尝试设置图表的plotarea.width属性的子例程中遇到错误。
如果我注释掉前面的行,其他尺寸也会导致此错误。
没有ActiveChart,没有选择等。特定的错误消息是这样的:“-2147467259(80004005)对象'PlotArea'的方法'Width'失败”
这让我很沮丧,原因有几个:
有什么想法吗?这是我可以共享的代码,全部包含ChartSizeMedium子例程,还有一个虚拟代码段,向您展示如何建立图表并将其传递给该子集,该子集设置大小和其他一些属性,然后再传递给另一个函数将系列数据添加到图表。
Option Explicit
Private Sub EstablishChartObject()
Dim cObj as ChartObject
Set cObj = ActiveSheet.ChartObjects.Add(Left:=30, Top:30, Width:=740, Height:=300)
ChartSizeMedium cObj.Chart, "Integer", "Example Chart Title"
End Sub
Private Sub ChartSizeMedium(cht As Chart, NumType As String, Optional chtTitle As String)
'Subroutine to make a consistent size chart
Dim s As Long
With cht
'Add a chart title if one exists.
If Len(chtTitle) > 0 Then
.HasTitle = True
.chartTitle.Characters.Text = chtTitle
End If
'Create the default chart Legend
.HasLegend = True
With .Legend
.Position = xlTop
.Font.Size = 11
.Font.Bold = True
End With
'Format the axes
.Axes(xlValue).MajorGridlines.Format.Line.Visible = msoFalse
.Axes(xlValue).MinorGridlines.Format.Line.Visible = msoFalse
'Format the size of the chart
With .Parent
.Width = 740
.Height = 396
End With
With .PlotArea
.Width = 640 '<---- THIS LINE TRIGGERS THE ERROR
.Height = 280
.Left = 30
.Top = 30
End With
End With
'Some charts start with more than one series container, so make sure they're gone:
With cht
Do Until .SeriesCollection.Count = 0
s = .SeriesCollection.Count
.SeriesCollection(s).Delete
Loop
End With
End Sub
更新2012年12月12日
我删除了所有非问题代码,并仅使用带有块的PlotArea,在同一例程中,我还尝试设置图表类型(几个值),并且如本示例中所示,在尝试设置之前手动添加了一系列数据PlotArea尺寸,但错误仍然存在:
Option Explicit
Private Sub EstablishChartObject2()
Dim cObj As ChartObject
Dim sh As Worksheet
Set sh = ActiveSheet
Dim srs As Series
Set cObj = sh.ChartObjects.Add(Left:=30, Top:=30, Width:=740, Height:=300)
Set srs = cObj.Chart.SeriesCollection.NewSeries
srs.Values = "={1,3,5,7,4}"
cObj.Chart.ChartType = 57
With cObj.Chart.PlotArea
.Width = 100 '<---- THIS LINE TRIGGERS THE ERROR
.Height = 280
.Left = 30
.Top = 30
End With
End Sub
最佳答案
我也有类似的问题。而这绝对是一个出色的问题(2013年)。
With .PlotArea
.Select 'err if delete this line of code
.Top = 0
.Left = 0
.width = 40
.Height = 40
End With
如果删除
.select
行,则在下一行将导致错误。请注意,我不使用<
with selection
做东西>。.select
使它正常工作,而无需使用选择,而wicht显然是一个Excel错误(来自以前的版本?)