+ Reply to Thread
Results 1 to 9 of 9

Use of Variants

  1. #1
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Use of Variants

    Hi,
    I rather not use Variants.
    In my C++ programming time typecasting could cause unexpected and unwanted results.
    Can someone point out the (absolute) need for the Variant type.
    Gr,
    Ricardo
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    1. To receive an argument that may be of varying type (string, Boolean, number, or arrays of same)

    2. To receive an indefinite number of arguments (ParamArray is a variant)

    3. To receive the results from certain functions, like MMult() (I'd have thought that a dynamic array of Doubles would work, but I couldn't get it to) or Array()

    4. To receive the value of a property that may return Null (e.g., selection.interior.colorindex for a non-homogeneous selection)

    5. To receive the keys of a dictionary.

    6. ...

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    6. To be the control variable of a For Each loop

    7. To be the return type of a Function that returns an array of variable size.

    8. To be a variable that derives its value from a worksheet cell.

    9. ...
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Mike,

    Thanks for continuuing the list -- I think it has sustaining value.

    7. To be the return type of a Function that returns an array of variable size.
    Not sure I agree with that; a dynamic array can be assigned a variable-size array -- for example, the results of Split. Maybe some narrow-fication?

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I was thinking a UDF

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    That last UDF runs into some differences between Mac VBA and Windows VBA, but Variant is the required type for a Mac. (VBA v.5 instead of v.6 ?? )
    Last edited by mikerickson; 09-13-2008 at 10:38 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Mike,

    I can't speak to differences between Windows and Mac, but in the second case, in Windows, you can declare
    Please Login or Register  to view this content.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    That is the part of the difference.
    That statement will give an "Expected: end of statement error" to a Mac.
    Also, this will work on Windows:
    Please Login or Register  to view this content.
    but gives a Mac a "Can't assign to array" error. The fix:
    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    re: Use of Variants

    Thank you for all the answers.
    Certainly helps.
    Last additonal question. have you ever seen that the typecast failed, e.g. for MMult, Cint(), SumProduct, ... Functions?

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I'm not sure what you mean by "typecast failed". I've had problems with all those functions, usually with the arguments.

    BTW, for my situation, WorkSheetFunction.Sumproduct fails and Evaluate("=Sumproduct...") needs to be used in VB routines.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Set background colour depending on variants
    By cannon_lab in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-18-2007, 10:30 AM
  2. IF condition to return 3 variants
    By Marie1uk in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-06-2007, 04:56 PM
  3. Limits on Variants, strings, etc?
    By ilovedurango in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2007, 02:25 PM
  4. count # of variants in a list
    By nolloping in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-31-2007, 11:36 PM

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