Search code examples
vba32bit-64bitvariant

Can you store a LongLong in a Variant?


According to the table in this article, the Variant type does not support VARENUM.VT_I8 - this maps onto VBA's VbVarType.vbLongLong. That suggests to me that a Variant can't hold a LongLong - however a LongLong is only as wide as a Double or Currency - and these can be stored in a Variant fine, so I see no reason why LongLong couldn't be, and I wonder if that article is out of date.

Anyway, I don't have a 64-bit Excel, so could someone please check for me - and if not supported then any thoughts why?


Solution

  • Based on this link that braX posted, my 64-bit Excel was able to run the following code.

    Option Explicit
    
    Public Sub ShowThatVariantsCanBeLongLong()
        Dim myLongLong As Variant
    
        myLongLong = 9.22337203685478E+18
    
        Sheet1.Cells(1, 1).Value = myLongLong
    End Sub
    

    Here's proof of the bitness of my excel app.

    enter image description here

    EDIT:

    Greedo commented that myLongLong might have been casted to a double, so I added the ^ specification to the number I assigned to myLongLong.

    Here's the output with the code I originally posted with the type name included for myLongLong.

    enter image description here

    So it looks like Variant will default to type double, but that doesn't necessarily answer the question.

    Here's the modified code that I used:

    Option Explicit
    
    Public Sub ShowThatVariantsCanBeLongLong()
        Dim myLongLong As Variant
    
        myLongLong = 9223372036854775807^
    
        Sheet1.Cells(1, 1).Value = myLongLong
        Sheet1.Cells(1, 2).Value = TypeName(myLongLong)
    End Sub
    

    Here was the output with the code in the screenshot as well:

    enter image description here

    It looks like Variant does support LongLong.