【发布时间】: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