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

ここで、別のサブルーチンからクラス module の値にアクセスしたいと思いますが、ここで MsgBox isc(8).secId (型の不一致) 行でエラーが発生します。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 に答える