title: VBA: Use a variable as a reference to a user defined type
body: <p>How can I reference a user defined type using a local variable without creating a copy of the type instance? </p>
  
  <p>As an example, in the code below what I would ideally like to do is in MySub3 where I create a local variable, <strong>MT</strong>, and reference a data structure nested inside another struct ... but VBA doesn't allow this. It allows it for objects but not for user defined types (arrggg!) ... and for no apparent reason ... it just doesn't allow it.</p>
  
  <p>MySub1 shows how to reference the nested struct in a long clunky way.
  MySub2 shows how to do this by passing in the nested struct, but this clutters up the calling routine, and having multiple such nested structs gets ugly.</p>
  
  <p>MySub2 demonstrates that VBA <em>can</em> do what I want, it just doesn't seem to provide a way to do it. I'm hoping there is a method I just haven't stumbled upon.</p>
  
  <p>Note that my actual code is MUCH more complicated than this example, with multiple independent structs providing indices to many arrays as struct elements. Using these local reference variables would make the code much more readable and manageable. </p>
  
  <p>Also Note that I am aware of the "with" statement, and it does help, but can only be used on one struct at a time. </p>
  
  <p>Also Note that I am aware that I could use an actual object class. My code started out using an object but I quickly found out that VBA places limitations on arrays as property members ... a limitation that user defined types don't have.</p>
  
  <pre><code>Type tMyType
      VariableA As Single
  End Type
  
  Type tMyOtherType
      MyTypeArray() As tMyType
  End Type
  
  Type tOneMoreType
      MyOtherType As tMyOtherType
  End Type
  
  Dim GlobalIndex As Integer
  
  Sub TopLevel()
      Dim TopLevelType As tOneMoreType
  
      ReDim TopLevelType.MyOtherType.MyTypeArray(0 To 10)
      Call MySub1(TopLevelType)
      Call MySub2(TopLevelType.MyOtherType.MyTypeArray(GlobalIndex))
      Call MySub3(TopLevelType)
  End Sub
  
  Sub MySub1(OMT As tOneMoreType)
      Dim VarA As Single
  
      VarA = OMT.MyOtherType.MyTypeArray(GlobalIndex).VariableA
  End Sub
  
  Sub MySub2(MT As tMyType)
      Dim VarA As Single
  
      VarA = MT.VariableA
  End Sub
  
  Sub MySub3(OMT As tOneMoreType)
      Dim VarA As Single
      Dim MT
  
      Set MT = OMT.MyOtherType.MyTypeArray(GlobalIndex)
      VarA = MT.VariableA
  End Sub
  </code></pre>