+ Reply to Thread
Results 1 to 9 of 9

returning user-defined type in array from function?

  1. #1
    Registered User
    Join Date
    03-22-2009
    Location
    Antwerp
    MS-Off Ver
    Excel 2002 SP2
    Posts
    45

    returning user-defined type in array from function?

    I'd like to return an array with several variables of a user-defined type from a function. Couldn't find a solution!

    I get a Compile error on Function() As Variant:
    "Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions"
    and a Compile error on Function As Variant:
    "Type mismatch"
    Please Login or Register  to view this content.
    Any help is appreciated, thank you

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: returning user-defined type in array from function?

    You can get rid of that error by changing the function declaration of fTest to return typeX() instead of Variant.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-22-2009
    Location
    Antwerp
    MS-Off Ver
    Excel 2002 SP2
    Posts
    45

    Re: returning user-defined type in array from function?

    Thanks you!, SOLVED
    How could I not find this myselve!!!!

  4. #4
    Registered User
    Join Date
    03-22-2009
    Location
    Antwerp
    MS-Off Ver
    Excel 2002 SP2
    Posts
    45

    Re: returning user-defined type in array from function?

    This gives me an other question:

    Can I have 2 different types of user-defined variables info 1 array and return that from a function?
    So the function probably can't be declared as just 1 type...

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: returning user-defined type in array from function?

    declare it as Variant and then it can return anything :-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Registered User
    Join Date
    03-22-2009
    Location
    Antwerp
    MS-Off Ver
    Excel 2002 SP2
    Posts
    45

    Re: returning user-defined type in array from function?

    I get the error: Can't assign to array

    Please Login or Register  to view this content.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: returning user-defined type in array from function?

    my bad-that doesn't work in vba-you'd be better off using classes than udts

  8. #8
    Registered User
    Join Date
    03-22-2009
    Location
    Antwerp
    MS-Off Ver
    Excel 2002 SP2
    Posts
    45

    Re: returning user-defined type in array from function?

    I tried classes, but it's a pain in the ***.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: returning user-defined type in array from function?

    And using a UDT for this is a walk in the park?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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