Creating my own object in VBA

I've searched the forums, but I haven't found an answer or example in VBA. I'm trying to create an object, so I can wrote the program easily. In my object, there are variables, subs, functions and another objects as well. My problem is how to define the functions and subs. Here's my object:

Private Type T_Vertice 'My secondary object
     Moneyness As Double ' data
     Vol As Double ' more data
     Sub Add(ByRef Range_Vols As Range, ByRef Range_Moneyness As Range)
End Type
    
Private Type Dados_Meses    ' My main object.
     Data As String  ' more data
     Count As Long ' more data
     Vertice() As T_Vertice ' second object
     Sub Add(ByRef Range_Vols As Range, ByRef Range_Moneyness As Range)
End Type

The intention of my subs is to add, retrieve and sort data from the object (subs I'll write). I've read about the Property but I couldn't figure out how to use it properly (Property Get/Let/Set). Does anyone knows how can I declare the sub/function that is inside my object I was declaring like this, but is obviously wrong.

Private Sub T_Vertice.Add ()

...

End Sub

Thanks in advance =]

PS: Is that really an object I think I'm missing sth here...

 



Answer this question

Creating my own object in VBA

  • Kursat Konak

    Thx Andy, that helped.

    The only thing that is left is to create the Collection of Collections that I'll need. I'll post since it's done.


  • bharathi_tunes

    I think I may be going on the right path...

    I found sth (Jesus, I'm so new at VBA xD) called Collection. It permits you to add sth to a Collection (duh!). As a real life example, it's like a deck of cards. There's a method (Add) that adds cards to the deck, and another to remove cards from the deck, and another one (item) to search for an especific card (Item).

    The problem now is that I can't figure (yet) how to pass a user defined type to the Collection.

    Sub Obtem_Dados_Mes(ByRef Meses As Collections, ByVal Range_Vols As Range, ByVal Range_Moneyness As Range)
        Dim Mes_a_adicionar As T_Meses
        Dim Vertice_a_adicionar As T_Vertice
        For Cont_Coluna = 0 To 11
            For Cont_Linha = 0 To 6
               Vertice_a_adicionar.Vol = Range_Vols.Offset(Cont_Linha, Cont_Coluna).Value
               Vertice_a_adicionar.Moneyness = Range_Moneyness.Offset(Cont_Linha, Cont_Coluna).Value
               Call Meses.Vertice.Add(Vertice_a_adicionar, Range_Vols.Value)
            Loop
        Loop
    End Sub

    The bold line is where the error occurs. =/

     

    Ah, and the brand new user defined types are

    Private Type T_Vertice
            Moneyness As Double ' Guarda o moneyness
            Vol As Double ' Guarda a volatilidade para um dado moneyness ATM, 10%, etc
        End Type
       
    Private Type T_Meses    ' Create user-defined type.
        'Data As String  ' Guarda a data base
        'Count As Long ' Guarda o numero de vertices existentes
        Vertice As Collection ' Guarda os valores de cada vertice
    End Type

    aaand...

    Dim T_Meses As Collection

    And this is the error...

    Compilation Error:

    Only user defined types in modules of public objects can be converted to or from a variant ou passed to late-bound functions

    :../


  • fripper

    Hi,

    You need to use classes rather than Types.
    Have a look at this site for more information.
    http://www.cpearson.com/excel/ClassModules.htm


  • Creating my own object in VBA