【问题标题】:Correcting Property/method errors更正属性/方法错误
【发布时间】:2017-02-27 23:52:40
【问题描述】:

我有这段代码,但我不断在同一行收到属性/方法错误。见下文。我很确定这是我在继续完全运行代码之前需要解决的最后一个问题。非常感谢任何和所有帮助。

“volume CT”是 ChartSheet 的名称

SC2 = ActiveWorkbook.Sheets("Volume CT").SeriesCollection.Count


For i = 1 To SC2 'Run the loop for all the data series [volume]
Debug.Print SC2 'make sure program counts correct amt of series

Dim name As String
    name = .FullSeriescollection(i).name
    Debug.Print name
End With

       'Find series names and change color for solids & area RedZone1
 resultx = InStr(1, name, x, vbTextCompare)
       'x is the variable name for the "codeWord" to look for
 Debug.Print resultx
 If resultx <> 0 Then
 With ThisWorkbook.Sheets("Volume CT").Chart.SeriesCollection(i).ChartArea.Format.Fill

^^Error 438: Onject 不支持属性或方法

    .Visible = msoTrue
    .ForeColor.RGB = Red
    .Transparency = 0
    .Solid
End With
End If

【问题讨论】:

  • Sheets 对象是否有Chart 对象?在第一行,你正在做Sheets("Volume CT").Series...,但在错误行,你有Sheets("Volume CT"). Chart .Series...
  • @TylerStandishMan 差不多。 Sheets 对象是一个集合,它的 Item getter(此处隐式使用的默认属性)返回一个 Object,因此除此之外没有 IntelliSense。但是在这种情况下,返回对象的运行时类型是Chart...事实上,Chart 对象没有Chart 成员。
  • @Mat'sMug,谢谢。我没有靠近电脑,也有很长一段时间没有处理 VBA,但这对我来说似乎很奇怪,因为你得到了你已经提到的对象,就像你澄清的那样。

标签: vba methods charts properties formatting


【解决方案1】:

ThisWorkbook.Sheets("Volume CT") Chart 对象。而Chart 对象没有.Chart 成员。

如果您查看 Project Explorer,您会在 Microsoft Excel 对象 下看到您的所有工作表(和图表表),以及 ThisWorkbook。 p>

当您执行.Sheets("Volume CT") 时,不要访问ThisWorkbook.Sheets 集合并针对您正在使用的隐式Sheets.Item getter 返回的一些Object 进行后期绑定,使用图表对象本身 em> - 已经 是对它的全局引用,可以使用:

给它一个有意义的名字...

...然后使用它:

With WhateverTheChartNameIs.SeriesCollection(i).ChartArea.Format.Fill

现在,您的指令有 7 个点(SeriesCollection(i) 隐含为 SeriesCollection.Item(i)),并且在后期绑定的 Object 调用中,点数相当多:如果您不确定涉及哪些类型以及什么他们的成员是,这个错误438是一定会发生的。

所以你现在有一个Chart2,并且你想访问一个特定的系列; SeriesCollection.Item 属性返回一个Object,因此不要在不知道该属性是否实际存在的情况下保留点和编写代码,而是为系列声明一个变量:

Dim s As Series
Set s = Chart2.SeriesCollection(i)

然后你就可以了

With s.ChartArrea.Format.Fill

但是等等! Series 没有 ChartArea 属性!我们到底想做什么?格式化图表的图表区域 - 那为什么还要麻烦SeriesCollection

With Chart2.ChartArea.Format.Fill 'notice you get IntelliSense all the way
    .Visible = msoTrue
    .ForeColor.RGB = Red
    .Transparency = 0
    .Solid
End With

瞧!

【讨论】:

  • 所以这是我不知道也没有尝试过的东西,谢谢。现在我有:使用 Volume CT.SeriesCollection(i).ChartArea.Format.Fill
  • 并且代码仍然无法正常工作,它说它期待表达式或语法错误的结尾
  • 您需要使用 name identifier(name) 属性,如屏幕截图所示),而不是“工作表名称” -合法标识符中没有空格。
  • @CoreyChung 有两个名称:“代码名称”是您可以在代码中使用的全局标识符,“工作表名称”显示在项目资源管理器的括号中。您尝试使用“工作表名称”。
  • 我现在打开了图表工作表的属性并将其替换为:使用 Chart2.SeriesCollection(i).ChartArea.Format.Fill 现在它仍然无法正常工作,但我至少正在使用现在在代码中使用正确的名称。同属性错误
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 2018-03-07
  • 2023-03-15
  • 1970-01-01
  • 2021-07-20
  • 2012-09-10
相关资源
最近更新 更多