以整数形式获取属性引发运行时错误451:未定义属性let过程,并且属性get过程未返回对象

时间:2018-07-26 20:24:14

标签: excel vba excel-vba runtime-error

我编写了一个名为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是整数。

每次我都为每个循环运行宏时,都会得到

  

运行时错误451:未定义属性let过程,并获取了属性   过程未返回对象

此“获取属性”末尾:

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

我觉得自己不知所措了,只是返回一个整数。如果有人可以帮助我了解发生了什么,那将是惊人的。如果您需要更多信息,请告诉我,我试图保持简洁。

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

1 个答案:

答案 0 :(得分: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的属性,但我从未见过像这样索引它。我认为是VBA抱怨的财产。您是要这样做吗?

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