【问题标题】:Why does "With x.Chart" generate a Compile Error为什么“With x.Chart”会产生编译错误
【发布时间】:2021-08-18 19:50:35
【问题描述】:

另一个相同的图形问题 - VBA 不喜欢我在命名范围之前创建系列的方式。我还有 4 个变量:ChtL1Obj As ChartObject, ChtL1 As Chart, ChtL2Obj As ChartObject, ChtL2 As Chart。我编写了以下代码(从 L2 开始):

Range("Q31").Select

Set ChtL2Obj = ActiveSheet.ChartObjects.Add(Left:=1075, Width:=450, Top:=465, Height:=225)

Set ChtL2 = ChtL2Obj.Chart

With ChtL2.Chart
    .ChartType = xlXYScatter
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "QC Level 2"
    .SeriesCollection(1).XValues = RngL2X
    .SeriesCollection(1).Values = RngL2Y
End With

问题是,代码无法编译。在With ChtL2.Chart 我得到“编译错误:找不到方法或数据成员”。

哪里出错了?

【问题讨论】:

标签: excel vba variables scatter-plot


【解决方案1】:

这个错误正是 GSerg 建议的。

当您设置 ChtL2 时,您引用的 ChtL2 等于 ChtL2Obj.Chart

Set ChtL2 = ChtL2Obj.Chart

所以你需要把 With ChtL2.Chart 改为 With ChtL2

With ChtL2
    .ChartType = xlXYScatter
End With

表示与 ChtL2.ChartType = xlXYScatter 或 ChtL2Obj.Chart.ChartType = xlXYScatter 相同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多