Search code examples
c++vbasizeof

VBA Equivalent of Sizeof()?


Is there an equivalent of the C++ sizeof function in VBA?

The only remotely similar functions seem to be the Ubound and LBound operators on arrays.

Dim arr(1 to 4) as integer
MsgBox Ubound(arr)

But this is not really the same thing as the C++ code:

int arr[10];
std::cout << sizeof(std::string) << "\t" << sizeof(arr);

Solution

  • The few pointer-related functions that can be used in VBA are at: http://support.microsoft.com/kb/199824 No obvious equivalent of sizeof though

    For an array, you could potentially do something with VarPtr if you temporarily made the array one item longer and then shrank it back to the desired size:

    Sub foo()
    
    Dim arr() As Integer 
    Dim i As Integer
    
    ReDim arr(1 To 5)
    arr(1) = 12
    arr(2) = 456
    arr(3) = -41
    arr(4) = 17
    
    Debug.Print VarPtr(arr(1)) & "; " & VarPtr(arr(5)) & "; " & VarPtr(arr(5)) - VarPtr(arr(1))
    
    ReDim Preserve arr(1 To 4)
    
    End Sub