0

我需要以下帮助。我制作了一个名为 CMpos 的类模块:

Public secId As String

然后在一个模块中,以下代码运行良好:

   Sub testclass()

rijaantal_LenDump = Application.CountA(Sheets("Len_Dump").Range("A:A"))
kolomaantal_LenDump = Application.CountA(Sheets("Len_Dump").Range("1:1"))

Sheets("Len_Dump").Select
positions = Sheets("Len_Dump").Range(Cells(1, 1), Cells(rijaantal_LenDump, kolomaantal_LenDump))


kolomSecID = 8

Dim isc As New Collection

For i = 1 To rijaantal_LenDump
Set psecs = New CMpos
psecs.secId = CStr(positions(i, 8))
If Not Exists(isc, psecs.secId) Then isc.Add psecs, psecs.secId
Next i

Debug.Print isc.Count

MsgBox isc(8).secId

End Sub

现在我想从另一个 sub 访问类 Moldule 中的值,但是在这里我在 MsgBox isc(8).secId (type mismatch) 行上得到一个错误。我在一个单独的模块中使用 Public isc As Collection 行创建了一个全局变量。

Sub hjhk()
Call testclass
Dim isc As CMpos

Set isc = New Collection


MsgBox isc(8).secId
End Sub

我究竟做错了什么?

谢谢阿米尔

4

1 回答 1

1
Dim isc as Collection 'global variable

Sub testclass()

    Dim psecs as CMpos

    Set isc = New Collection 'isc refers to the global variable,
                             '  so no need to declare it here

    For i = 1 To 8
        Set psecs = New CMpos
        psecs.secId = "Test-" & i
        isc.Add psecs, psecs.secId
    Next i
End Sub

Sub Test2()
    testClass
    Debug.Print isc(8).secId 'sic is declared as global, so no need to declare/create
End Sub
于 2013-06-07T16:50:26.683 回答