I have a class Car with properties model and year. I would like to create a function that create Car objects, set their properties using data from a Sheet through for loop, and, finally, return an array of Car objects. After that, I would like to create a sub that fills the list of a ListBox with an array returned by the function mentioned previously.
The code I created for the Function won't work as it returns error 91 when executing the last line. In case I add a "Set" to that line, the error changes to "Object Required". Would anyone fix this code out or maybe let me know about a better idea to do what I am willing to? The codes of both the function and the sub procedure follows:
Function StoreAllCars() As CarDim intLastRow As Integer: intLastRow = 5
Dim CarArray() As New Car: ReDim CarArray(0)
Dim DataSheet As Worksheet
Set DataSheet = Sheet3
For i = 2 To intLastRow
Dim MyCar As New Car
MyCar.Model = DataSheet.Cells(i, 1)
MyCar.Year = DataSheet.Cells(i, 2)
Set CarArray(i - 2) = MyCar
ReDim Preserve CarArray(i - 1)
Next i
ReDim Preserve CarArray(i - 3)
StoreAllCars = CarArray
End Function
Sub PopModelsListBox()
UserForm1.ListBox4.List = StoreAllCars.Model
End Sub
Jorge Barbi Martins (jorge.barbi@hotmail.com)