Quantcast
Channel: General Office Development forum
Viewing all articles
Browse latest Browse all 2257

VBA: How to work with arrays of objects that have multiple properties?

$
0
0

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 Car
Dim 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)


Viewing all articles
Browse latest Browse all 2257

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>