gpt4 book ai didi

vba - 在 Excel、VBA (Excel 2010) 中设置 PlotArea.Width 时出错

转载 作者:行者123 更新时间:2023-12-02 07:28:27 30 4
gpt4 key购买 nike

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

enter image description here

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

这让我感到困惑有几个原因:

  • 在 Debug模式下,按 F8 单步执行代码不会发生错误。
  • 据我所知“宽度”不是一种“方法”,而是图表绘图区域的“属性”,因此即使错误消息也相当模糊。

有什么想法吗?这里有我可以分享的尽可能多的代码,完整的 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 日更新

我删除了所有无问题的代码并仅使用带有 block 的 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

最佳答案

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

With .PlotArea 
.Select 'err if delete this line of code
.Top = 0
.Left = 0
.width = 40
.Height = 40
End With

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

关于vba - 在 Excel、VBA (Excel 2010) 中设置 PlotArea.Width 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812491/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com