我在子程序中尝试设置图表的plotarea.width属性时遇到了错误。
如果我注释掉前面的行,其他维度也会导致此错误。没有ActiveChart、选择等。具体的错误消息是这样的:"-2147467259 (80004005) Method 'Width' of object 'PlotArea' failed"
这让我感到困惑的原因有几个:
- 在调试模式下,通过F8逐步执行代码时不会出现错误。
- 据我所知,“width”不是图表的plotarea的“方法”,而是“属性”,因此即使是错误消息本身也相当模糊。
有什么想法吗?以下是我能分享的所有代码,完整的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
ScreenUpdating
来解决了这个问题:http://www.vbaexpress.com/forum/archive/index.php/t-17251.html。不知道是否适用于你。 - Doug Glancywidth
代码,看看是否会出现错误在其它行吗? - bonCodigowidth
,那么下一行的.height
就会出现错误。错误代码和信息是完全模糊的。这对你有帮助吗?我似乎可以通过选择绘图区域,然后立即取消选择(这样错误就不会跳到height
),但我希望能在不进行“选择”的情况下完成此操作。 - David Zemens