I'm just learning Excel VBA and I'm missing some concept here I think.
Let's say I've a table of 5 car brands. I want to read those into my own class module of car brands, then print their names.
I've two ways to do this, and only one works. I'm wondering if anyone could explain why?
Working Version:
dim colCarBrands as new collection
For i = 1 To 5
Dim newCarBrand As clsCarBrand
Set clsCarBrand = New clsCarBrand
newCarBrand.BrandName chaletsSheet.Cells(i, 1)
colCarBrands.Add newCarBrand , newChalet.nameTechnical
Next
Dim b As clsCarBrand
For Each b In colCarBrands
' Print items
Debug.Print b.BrandName
Next
Not Working Version:
dim colCarBrands as new collection
For i = 1 To 5
Dim newCarBrand As New clsCarBrand
newCarBrand.BrandName chaletsSheet.Cells(i, 1)
colCarBrands.Add newCarBrand , newChalet.nameTechnical
Next
Dim b As clsCarBrand
For Each b In colCarBrands
' Print items
Debug.Print b.BrandName
Next
In this version, all car brands in the collection are the same one (it's the last one from the sheet)
Any ideas why?
Thanks!