【问题标题】:Get Property As Integer throws Runtime Error 451: Property let procedure not defined and property get procedure did not return an objectGet Property As Integer 引发运行时错误 451:Property let procedure not defined and property get procedure didn't return an object
【发布时间】:2018-07-26 20:24:13
【问题描述】:

我编写了一个名为 TimeManagementTable 的类模块来抽象出我正在使用的表的一些数据。这是代码:

Option Explicit
Private table As ListObject
Private idColumn As Integer
Private subColumn As Integer
Private descColumn As Integer
Private epColumn As Integer

' ERROR -1: No Table
' ERROR -2: Columns Not Found
Public Function init(newSelection As Range) As Variant
    Dim column As ListColumn
    Dim msg As Variant

    If newSelection.ListObject Is Nothing Then
        msg = MsgBox("Table not detected!", vbCritical, "Aborted")
        init = -1
        Exit Function
    End If
    Set table = newSelection.ListObject
    For Each column In table.ListColumns
        Select Case column.Name
            Case "ID"
                idColumn = Int(column.Index)
            Case "SUB"
                subColumn = Int(column.Index)
            Case "Description"
                descColumn = Int(column.Index)
            Case "EP"
                epColumn = Int(column.Index)
        End Select
        If (idColumn > 0 And subColumn > 0 And descColumn > 0 And epColumn > 0) Then
            Exit For
        End If
    Next
    If (idColumn = 0 Or subColumn = 0 Or descColumn = 0 Or epColumn = 0) Then
        msg = MsgBox("Either EP, ID, SUB, or Description column not detected!", vbCritical, "Aborted")
        init = -2
        Exit Function
    End If
End Function

Public Property Get TableObject() As ListObject
    Set TableObject = table
End Property

Public Property Get IDColumnInde() As Integer
    IDColumnInde = idColumn
End Property

Public Property Get SUBColumnInde() As Integer
    SUBColumnInde = subColumn
End Property

Public Property Get DescColumnInde() As Integer
    DescColumnInde = descColumn
End Property

Public Property Get EPColumnInde() As Integer
    EPColumnInde = epColumn
End Property

我调用了第一个 Get 属性,如下所示:

Set newSelection = Application.InputBox("Please enter which Time Log table you would like to add a row to:", "Insert New Entry", "DeliverableTimeLog", , , , , 8)
Set tmt = New TimeManagementTable
If (tmt.init(newSelection) < 0) Then Exit Sub
For Each row In tmt.TableObject.ListRows
   if row.Range.Formula(1, tmt.IDColumnIndex) = choiceID

其中 tmt 是 TimeManagementTable 的对象,row 是 ListRow 对象,choiceID 是整数。

现在我每次使用 for each 循环运行宏时都会得到

运行时错误 451:未定义属性让过程和属性获取 过程没有返回对象

在此获取属性的末尾:

Public Property Get IDColumnIndex() As Integer
    IDColumnIndex = idColumn
End Property

我觉得我正在失去理智,它所做的只是返回一个整数。如果有人能帮助我了解发生了什么,那就太棒了。如果您需要更多信息,请告诉我,我尽量保持简洁。

编辑:完整的类模块源代码和初始化

【问题讨论】:

  • 对不起,这是索引。在我尝试过的事情之一的代码中都是一样的,因为我认为它可能不喜欢我在标识符中有索引。我知道这很愚蠢,但这大约需要 3 个小时才能弄清楚,哈哈
  • 如果你把Option Explicit 放在你的类模块的顶部,它会编译吗?这是全班的清单吗?
  • @MathieuGuindon 它确实编译并运行并抛出相同的错误。
  • tmt 是什么?怎么设置的?
  • 但是您没有向我们展示您的实际类模块(无法分配封装的值)。你得到的错误表明一个错字或类似的东西。不要给我们代码截图,我无法将截图粘贴到VBE中。

标签: vba excel runtime-error


【解决方案1】:

该错误基本上是说您正在尝试分配给Property Get 成员。

SomeGetter = 42

这是非法的,因为您不能分配给没有 Property Let 访问器的属性。

关于“不返回对象”的部分是因为:

SomeGetter = 42

合法,如果SomeGetter 返回一个具有可分配给 RHS 值的默认成员的对象。换句话说,因为 VBA 看不到这个:

SomeGetter.DefaultProperty = 42

然后它会抛出一个错误,因为SomeGetter 不能是赋值的 LHS。寻找错字。确保每个模块都在顶部显示Option Explicit,尤其是类模块和带有引发错误的循环的模块。

您的帖子看起来包含多个混合版本的代码,因此很难准确判断问题出在哪里。


其实……

if row.Range.Formula(1, tmt.IDColumnIndex) = choiceID

Formula 是一个Variant 属性,但我从未见过它被这样编入索引。我认为 that 是 VBA 抱怨的属性。你是故意的吗?

if row.Range.Cells(1, tmt.IDColumnIndex).Value = choiceID

【讨论】:

  • getter 或者getter 的返回值从不与等号运算符接触,返回值"row.Range.Formula(int, int)" 是。它也在 if 语句的范围内,所以它是一个相等运算符而不是一个赋值运算符,对吧?
  • 正确。 VBE 在抛出错误时不是选择了错误的语句吗?
  • 等等Range.Formula(int,int) 应该如何工作?我只见过Range.Formula 分配/比较一个字符串...你是在比较某个值,你的意思是比较.Value?我认为 Formula(int,int) 编译只是因为它是 Variant 并因此在运行时进行评估。比较是非法的,错别字和吸气剂是红鲱鱼。
  • 是的,最后一部分就是这样。抱歉,我正在学习使用 VBA 并使用 .Formula 以前为我工作过。我用 row.Range.Cells(int,int).value 替换了它,它起作用了。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-11-05
  • 2019-02-25
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
相关资源
最近更新 更多