我正在写我的第一堂课。一个是 cCRElist,它本质上是 cCRE 实例(一些特殊事件)的集合。
我希望在 cCRElist 中有一个子函数或函数,它将工作表中的所有 CRE 加载到我可以使用的一个大集合中。我创建了这个函数,当我从一个普通的代码模块调用它时它工作正常,但后来我试图将代码移动到类中。现在我无法调用函数 LoadFromWorksheet(myWS as Worksheet)。
错误是“对象不支持此属性或方法”。我曾尝试将其设为子程序、函数,将其设为公开而非公开,我已尝试将其变为 Property Let 而不是子程序。显然,我对它的作用一无所知。我努力了
Call CREList.LoadFromWorksheet(myWS)
和
CREList.LoadfromWorksheet myWS
每次都是同样的错误。
这是使用该类并调用该函数的测试代码:
Sub TestClassObj()
Dim CRElist As cCRElist
Set CRElist = New cCRElist
Dim myWS As Worksheet
Set myWS = ThisWorkbook.ActiveSheet
CRElist.LoadFromWorksheet (myWS)
End Sub
这是 cCRElist 类的一个片段:
' **** CLASS cCRElist
Option Explicit
' This is a collection of CRE objects
Private pCRElist As Collection
Private Sub Class_Initialize()
Set pCRElist = New Collection
End Sub
Public Property Get CREs() As Collection
Set CREs = pCRElist
End Property
Public Property Set Add_CRE(aCRE As cCRE)
pCRElist.Add aCRE
End Property
Function LoadFromWorksheet(myWS As Worksheet)
Dim CRE As cCRE
Dim iFirst As Long
Dim iLast As Long
Dim i As Long
Set CRE = New cCRE
iFirst = gHeader_Row + 1
iLast = FindNewRow(myWS) - 1
' update data in CRE then add
For i = iFirst To iLast
If myWS.Cells(i, gCRE_Col) <> "" Then ' This is a CRE row
Set CRE = New cCRE
With CRE
.CRE_ID = myWS.Cells(i, gCRE_Col)
If Not IsDate(myWS.Cells(i, gCRE_ETA_Col)) Then
.ETA = "1/1/1900"
Else
.ETA = Trim(myWS.Cells(i, gCRE_ETA_Col))
End If
<... snipped ...>
End With
pCRElist.Add_CRE CRE
End If
Next
End Sub
' **** END OF CLASS cCRElist
感谢您的专业知识。