Excel VBA

I am using a user-defined type. Is it possible to access the individual variables with an index number rather than by the variable name itself

For e.g.:

Type DescType
PN As String
Qty As Integer
Check As Boolean
End Type

Dim DescVar as DescType

I want to refer to var PN as DescVar(1) or DescVar[1] or DescVar.1 ,etc and not as DescVar.PN

Thanks,

enkay



Answer this question

Excel VBA

  • Zygimantas

    Hi enkay,

    Me is a keyword in VBA that allows you to reference the object the code is in. It's not 100% required and its just a habit I've got.

    Me.txtTextBox.Text.... Me would refer the userform that txtTextBox is on.

    Me.Cells.... Me would refer to a worksheet

    Its just a way to refer to the object in which the code is written. Me in the code example refers to the Class Module.



  • ncarty97

    Derek,

    Thank you for the quick reply and suggestion.

    I have not used the Class module so far. In the solution, how is "Me" declared


  • ___MLyons___

    Hello,

    You could use a class module instead of a custom type and have a Item property that takes the index of the item you want and returns the variable... like this..

    Public PN As String
    Public Qty As Integer
    Public Check As Boolean

    Public Property Get Item(Index As Integer) As Variant
    Select Case Index
    Case 1
    Item = Me.PN
    Case 2
    Item = Me.Qty
    Case 3
    Item = Me.Check
    End Select
    End Property

    You will have to treat the variables as variants because they are different types.



  • Excel VBA