2013-08-23 76 views
2

我有一個包含它自己的集合的類。 (頂級類別包含集合中的詳細實例的摘要版本。)Excel VBA:如何將項目添加到課程中的集合中?

當前集合是一個公共變量,因爲我還沒有弄清楚使用私有變量的所有細節。我可以稍後解決。

如何添加項目到集合中?我收到錯誤91的缺少對象變量。

感謝您的所有幫助。我一直在重新調整我的代碼以更廣泛地使用類,而且事情的清理真的很棒。

調用這個類CPE

Public PE_Details As Collection ' collection of cPE 
Public PE_ID as integer 
Public PE_ID_Index as integer 

' Add to the detailed list of PE's 
Public Function AddPEDetail(ByRef cPE_Detail As cPE) 

    PE_Details.Add cPE_Detail ' ERROR: Object variable or With 
           ' block variable not set 

End Function 

模塊代碼如下:

Dim clsPE As cPE    ' Summary version of PE 
Dim clsPE_Detail As cPE   ' A detailed PE 
Dim i as Integer 

Set clsPE = New cPE  ' This is the PE which will also contain a list of detailed PEs 

' Add three instances of detailed cPE to the summary cPE object 
for i = 1 to 3 
    Set clsPE_Detail = New cPE 

    clsPE_Detail.PE_ID = clsPE.PE_ID 
    clsPE_Detail.PE_ID_Index = clsPE.PE_ID_Index 
    'etc. 

    clsPE.AddPEDetail clsPE_Detail ' see above 
next i 
+0

您在哪裏設置PE_Details? –

回答

7

在您的CPE類中添加方法Class_Initialize和初始化變量。因爲您現在擁有它,所以您從未設置過PE_Details,因此它爲空/無

Private Sub Class_Initialize() 
set PE_Details = New Collection 
End Sub 
相關問題