A vector of vectors (vectors Collection)

Hi...

I need to create a vector of vectors (or a vectors container) using VBA for Excel. How can i do this

Thanks..



Answer this question

A vector of vectors (vectors Collection)

  • nullptr

    Thanks for your answer, it was so helpfull.

    What i finally did is:

    1) Create a new user data type

    Private Const NUMERO_DE_MUNICIPIOS = 12, NUMERO_ESPECIES = 9
    Type ContenedorEmisiones

    Vector(1 To NUMERO_ESPECIES) As Double
    End Type

    2) Declare a Vector with my new data type

    Dim EmisionesAnuales() As ContenedorEmisiones

    3) Finally i use it like this:

    For j = 1 To AgnoFinInv - AgnoInicioInv + 1
    For k = 1 To NUMERO_ESPECIES

    EmisionesAnuales(j).Vector(k) = Emisiones(k)

    Next k

    Next j

    An that's it. A Collection of Vectors.


  • Liam404

    Glad to be of service. Just a note on the Collection object -- it's much more flexible than setting up an array. For example, let's say you've got an array dimensioned with 50 elements, of which 20 are occupied. If you want to delete the 10th element of the array, you need to manually move each element of the array from the 11th to the 20th into the previous "slot".

    With a Collection, you don't need to muck around with this kind of thing. You can simply use the Add and Remove methods to work with the Collection's contents, without needing to worry about which item appears in precisely what location.

    Anyway, glad I was able to help



  • ClaudiaHelpOnVSTO

    I'm not clear on just what you're trying to do here. Are you looking to build a Collection of vectors If so, you can do something like this in the general code area of one of your modules:

    Type Vector

    Direction as Single

    Magnitude as Single

    End Type

    Private MyVectors As Collection

    And then code like this could be used to initialize and access the members of the Collection.

    Private Sub InitMyVectors()

    Dim tvector as Vector

    tvector.Direction = 0.50

    tvector.Magnitude = 2.50

    MyVectors.Add tvector, "Vector1"

    MsgBox "Vector 1 = (" & MyVectors("Vector1").Direction & ", " & MyVectors("Vector1").Magnitude & ")"

    End Sub

    Hope this helps. If this isn't what you're looking for, please post to clarify and I'll try to be of further assistance.



  • A vector of vectors (vectors Collection)