3

C++ sizeofVBA中是否有等效的函数?

唯一远程相似的函数似乎是数组上的Uboundand运算符。LBound

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

但这与 C++ 代码并不完全相同:

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

4 回答 4

5

我意识到这已经很晚了,但是......你想要的是LenB()

Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Debug.Print LenB("a") '==>2 i.e. ["a",0&]

Dim r as RECT
debug.print LenB(r) '==>16 i.e. 4 bytes for each long
于 2019-01-06T16:45:24.033 回答
2

会有Len 函数,但对于字符串,它将无法工作,SizeOf因为它会逐字检查字符串变量中包含的字符串的长度。

因此,要检查整数数组的字节大小:

Dim arr(1 To 4) As Integer
Debug.Print (UBound(arr) - LBound(arr) + 1) * Len(arr(LBound(arr)))

立即窗口将显示: 8

于 2014-03-14T19:30:35.020 回答
2

可以在 VBA 中使用的几个与指针相关的函数位于:http: //support.microsoft.com/kb/199sizeof ​​824虽然没有明显的等价物

对于数组,VarPtr如果您暂时将数组延长一项,然后将其缩小到所需的大小,您可能会做一些事情:

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
于 2012-09-12T01:45:23.543 回答
0

如VBA 参考手册中所定义。

您可以使用LENLENB


示例LEN

Type CustomerRecord    ' Define user-defined type.
    ID As Integer    ' Place this definition in a 
    Name As String * 10    ' standard module.
    Address As String * 30
End Type

Dim Customer As CustomerRecord    ' Declare variables.
Dim MyInt As Integer, MyCur As Currency
Dim MyString, MyLen
MyString = "Hello World"    ' Initialize variable.
MyLen = Len(MyInt)    ' Returns 2.
MyLen = Len(Customer)    ' Returns 42.
MyLen = Len(MyString)    ' Returns 11.
MyLen = Len(MyCur)    ' Returns 8.

示例LENB

Function LenMbcs (ByVal str as String)
    LenMbcs = LenB(StrConv(str, vbFromUnicode))
End Function

Dim MyString, MyLen
MyString = "ABc"
' Where "A" and "B" are DBCS and "c" is SBCS.
MyLen = Len(MyString)
' Returns 3 - 3 characters in the string.
MyLen = LenB(MyString)
' Returns 6 - 6 bytes used for Unicode.
MyLen = LenMbcs(MyString)
' Returns 5 - 5 bytes used for ANSI.
于 2019-09-13T16:53:57.863 回答