+ Reply to Thread
Results 1 to 3 of 3

Is it possible to combine 2 User Defined Types?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-25-2011
    Location
    Virginia
    MS-Off Ver
    Excel 2016
    Posts
    166

    Is it possible to combine 2 User Defined Types?

    i have two udts that I would like to combine using conditional compilation. I have tried multiple methods and search the web to no avail. Below is a sample of the two types i need to combine (note the real UDTs are DEVMODE APIs and 30+ lines long, just using this sample to make it easier to follow)
    Type Test1
      test1 As Long
      test2 As String
      test3 As Integer
    End Type
      
    Type TestAdd1
      test4 As String
      test5 As Integer
    End Type
    The result I would like to achieve after combining the two types would be:
    Type Test1
      test1 As Long
      test2 As String
      test3 As Integer
      test4 As String
      test5 As Integer
    End Type
    There are actually 4 types of TestAdds - TestAdd1, TestAdd2 ect. The one that gets added to the bottom of Test1 is based on a conditional complie statement - I have that part working - I just can't figure out how to put TestAdd1 on the bottom of Test1. I have a work around of defining 4 different types, each one starting off with Test1 and then followed by each of the TestAdds but that adds over 100 lines of code and doesn't seem like good programing practice to me.

    So is there any way to combine the two types?

    Thanks in advance,

    Tom
    Last edited by tom.hogan; 03-11-2012 at 09:08 AM.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Is it possible to combine 2 User Defined Types?

    Types can be nested.
    Type aPerson
        Name As String
        Height As Long
    End Type
    
    Type aTeam
        Captain As aPerson
        Member1 As aPerson
        Member2 As aPerson
    End Type
    
    Sub test()
        Dim myPerson As aPerson
        Dim myGroup As aTeam
        With myPerson
            .Name = "Mike"
            .Height = 72
        End With
        myGroup.Captain = myPerson
        MsgBox myGroup.Captain.Name
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    01-25-2011
    Location
    Virginia
    MS-Off Ver
    Excel 2016
    Posts
    166

    Re: Is it possible to combine 2 User Defined Types?

    Mike,

    That meet's my need, thanks a lot.

    Tom

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1