Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 固定字符串*7使用的内存是否比=Len(7)的整数少?_String_Excel_Types_Integer_Vba - Fatal编程技术网

String 固定字符串*7使用的内存是否比=Len(7)的整数少?

String 固定字符串*7使用的内存是否比=Len(7)的整数少?,string,excel,types,integer,vba,String,Excel,Types,Integer,Vba,固定字符串使用的内存会比整数少吗 Public Type MyType UserID As Integer 'All My User IDs are less than 7 integers in length UserID As String * 7 ''Will a Fixed String use less memory than an Integer? End Type 有没有办法在VBA过程中进行测试?字符串将使用更多内存 整数(根据示例应为长)为4字节,字符串至少为8 --根据

固定字符串使用的内存会比整数少吗

Public Type MyType

UserID As Integer 'All My User IDs are less than 7 integers in length
UserID As String * 7 ''Will a Fixed String use less memory than an Integer?

End Type

有没有办法在VBA过程中进行测试?

字符串将使用更多内存

整数(根据示例应为长)为4字节,字符串至少为8

--根据注释编辑lenB(var)将给出其大小的概念

Private Type MyType
    UserID  As Integer
    sUserID As String * 7
End Type

Dim x As MyType
    x.UserID = 123
    x.sUserID = "123"

Debug.Print LenB(yourType.UserID)     
Debug.Print LenB(yourType.sUserID)
Debug.Print LenB(yourType)

2 
14 
16 
因为字符串稍微复杂一点,它不仅仅是内存中的一系列字节,而是存储为BSTR,其开销为6字节(长度为4字节,每个字符2字节,终止符为\0\0)


但是你应该为你想要存储的数据选择正确的类型,而不是担心它们之间的差异——在你的例子中,它的12个字节在当今时代是完全不相关的。

在VBA中,整数是2个字节,长是4个字节。如果这个例子是错误的,因为2个字节不能容纳所有6位的整数,即999999。我的回答是基于实际描述,而不是示例中的错误。是的,我也这么想。我刚刚在链接文档中注意到,可变长度字符串的存储大小是“10字节+字符串长度”,但似乎并不准确,因为字符串每分钟使用2个字节character@Slai所有字符串现在是否每个字符使用2字节?我以为是Unicode字符做的。因此,
Chr
vs
ChrW
@RonRosenfeld我找不到好的参考,但是VBA对所有字符串都使用Unicode编码(VBA编辑器使用ANSI编码)。您还可以使用LENB()检查每个字符的字节数,或转换为字节数组。整数中可存储的最大数为32767(2个字节),长2147483647(4个字节)