在Excel,VBA(Excel 2010)中设置PlotArea.Width时出错

Dav*_*ens 5 excel vba excel-vba excel-2010

我在尝试设置图表的plotarea.width属性的子例程中遇到错误.

在此输入图像描述

如果我注释掉前面的行,其他维度也会导致此错误.没有ActiveChart,没有选择等.具体的错误信息是这样的:"-2147467259(80004005)对象'PlotArea'的方法'宽度'失败"

由于以下几个原因,这让我很难过:

  • 在调试模式下,F8逐步执行代码,不会发生错误.
  • AFAIK"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
Run Code Online (Sandbox Code Playgroud)

更新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
Run Code Online (Sandbox Code Playgroud)

Pat*_*ier 5

我有类似的问题。这绝对是一个Excel问题(2013年)。

With .PlotArea 
    .Select 'err if delete this line of code
    .Top = 0
    .Left = 0
    .width = 40
    .Height = 40 
End With
Run Code Online (Sandbox Code Playgroud)

如果删除该.select行,将导致下一行出现错误。请注意,我没有使用 < with selectiondo stuff>。这.select使得它可以工作,而不使用选择,这显然是一个Excel错误(来自以前的版本?)


Dav*_*ens 4

两个解决方案似乎都有效,但都不像我想要的那样“优雅”(我希望有一种方法可以通过选择图表或其任何部分来做到这一点)。

选项 1 - 选择绘图区域,然后取消选择。 这似乎是最可靠/最有效的解决方案。

With .PlotArea
    Application.ScreenUpdating = False
   .Select
    With Selection
        .Width = paWidth
        .Height = paHeight
        .Left = paLeft
        .Top = paTop
        ActiveSheet.Range("A1").Activate
    End With
    Application.ScreenUpdating = True
End With
Run Code Online (Sandbox Code Playgroud)

选项 2 - 禁用循环中的错误处理(这来自 Doug 的链接)。这似乎不是一个非常可靠或有效的方法,尽管它似乎有效,但我知道在该循环中,它在每个属性上失败一次,然后才能在后续传递中成功设置它们。

With .PlotArea
    For pLoop = 1 To 5
        On Error Resume Next
        .Width = paWidth
        .Height = paHeight
        .Left = paLeft
        .Top = paTop
        On Error GoTo 0
    Next
End With
Run Code Online (Sandbox Code Playgroud)