2015-11-06 60 views
0

這個想法很簡單,填充並顯示具有不同值的集合。但它只顯示最後一個值。我不確定問題是在填充子部分還是顯示部分。使用訪問VBA訪問VBA集合顯示不正確

public sub populate(list as Collection) 

dim i as integer 
for i = 0 to 100 
    dim e as new Numbers 
    e.addition = i+i 
    e.multiply = i*i 
    list.add e 
next 
end sub 

稱之爲:

Dim test As Numbers 
    Dim list as new Collection 
    Call populate(list) 
    For Each test In list 

     Debug.Print test.addition & ", " & test.multiply  

    Next test 

這隻會顯示最後的數字,對象列表收藏的100倍。據我所知

任何幫助表示讚賞

回答

0

Dim e as new Numbers不會每次都創建一個新元素,定義功能廣泛,所以你只需要編輯每次同Numbers e。移動外循環Dim e as Numbers和使用Set e as new Numbers循環內應該做的伎倆:

(未測試)

public sub populate(list as Collection) 

dim i as integer 
dim e as Numbers 
for i = 0 to 100 
    Set e = new Numbers 
    e.addition = i+i 
    e.multiply = i*i 
    list.add e 
next 
end sub 
+0

謝謝!問題解決了 –