Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: Select

[已解决]Len 函数的返回的问题

  [复制链接]
发表于 2011-12-30 12:04 | 显示全部楼层
Select 发表于 2011-12-29 22:49
谢谢 tianti !

自己理解比较差,看了4楼,还是没看懂你的说明。可否说明下。

个人感觉,也许有时候中文翻译不是很到位,有疑问时不妨看看微软英文的说明.

http://office.microsoft.com/en-us/excel-help/len-lenb-HP005209154.aspx

微软没有提到变量的问题,4楼的意思是这样:

Len("ABC") = 3

或者当定义变量时,

Dim i as String
        i = "ABC"
       Len(i) = 3 (注意这里不等于1)


excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

 楼主| 发表于 2011-12-30 12:30 | 显示全部楼层
本帖最后由 Select 于 2011-12-30 12:39 编辑
adders 发表于 2011-12-30 12:04
个人感觉,也许有时候中文翻译不是很到位,有疑问时不妨看看微软英文的说明.

http://office.microsoft.c ...


谢谢 adders !


A)直接len(字符串常量)
B)先将字符串常量赋值给字符串变量,再len(字符串变量)

11楼的意思是不是,无论哪种情况,都返回“包含字符串内字符的数目”?
为什么提到注意是否等于1?
等于1,意味什么?
什么情况,会等于1?

回复

使用道具 举报

发表于 2011-12-30 12:49 | 显示全部楼层
本帖最后由 adders 于 2011-12-30 00:07 编辑
Select 发表于 2011-12-29 23:30
谢谢 adders !


不知道中文版的帮助是否相同...我用的英文版,这是F1文件,以及示例.功能说明是:在文本中返回字符数,或在变量中返回储存变量要求的字节数.

例子中说明了String和Variant的处理是一样的,Integer返回2,Currency返回8,示例中用户自定义的CustomerRecord由于包括了Integer (2), Name (10), Address (30),所以len的返回值是2+10+30 = 42

Len Function

Returns a Long containing the number of characters in a string or the number of bytes required to store a variable.
Syntax
Len(string | varname)
The Len function syntax has these parts:
PartDescription
stringAny valid string expression. If string contains Null, Null is returned.
VarnameAny valid variable name. If varname contains Null, Null is returned. If varname is a Variant, Len treats it the same as a String and always returns the number of characters it contains.



Remarks
One (and only one) of the two possible arguments must be specified. With user-defined types, Len returns the size as it will be written to the file.
Note   Len may not be able to determine the actual number of storage bytes required when used with variable-length strings in user-defined data types.


Len Function Example
The first example uses Len to return the number of characters in a string or the number of bytes required to store a variable. The Type...End Type block defining CustomerRecord must be preceded by the keyword Private if it appears in a class module. In a standard module, a Type statement can be Public.

Type CustomerRecord    ' Define user-defined type.   
ID As Integer   
Name As String * 10  
Address As String * 30
End Type

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

评分

参与人数 1 +2 收起 理由
Select + 2 我就是懂不了英文555555

查看全部评分

回复

使用道具 举报

发表于 2011-12-30 13:01 | 显示全部楼层
Variant太tm特殊了!
以下是在楼主另一贴中的回复:
Sub test3()
    Dim MyIntegerArray, MyDoubleArray, MyVariantArray   
    ' 整型数组使用 22 个字节(11 元素* 2 字节)
    ReDim MyIntegerArray(10) As Integer
    Debug.Print Len(MyIntegerArray(0))    '返回1   
    ' 双精度数组使用 88 个字节(11 元素 * 8 字节)。
    ReDim MyDoubleArray(10) As Double
    Debug.Print Len(MyDoubleArray(0))    '返回1   
    ' 变体型数组至少使用 176 字节(11 元素 * 16 字节)。
    ReDim MyVariantArray(10)
    Debug.Print Len(MyVariantArray(0))    '返回0   
End Sub
你上面的结果非如你的结论!
Variant很特殊!
第一二返回的1,你可以看看他们的第0个元素是什么值,都是0,0是只有一个字符对吧?所以返回1。
而第三个呢?是空值,对吧?所以返回0
不信?你试试:MyIntegerArray(0)=123,再Debug.Print Len(MyIntegerArray(0)) 该返回多少?是3,MyIntegerArray(0)=1232就成了4,想到了吗?因为123是有三个字符、1232是有四个字符,而不看他是不是integer!晕不?这就是Variant的特殊!

评分

参与人数 1 +2 收起 理由
Select + 2 为什么说“你上面的结果非如你的结论!”呀

查看全部评分

回复

使用道具 举报

发表于 2011-12-30 13:54 | 显示全部楼层
看了高手们的回复,回过头来想,这次微软的帮助倒没太大问题,大部分问题在帮助中说明了
部分
参数类型
返回结果
 
Null
返回 Null
string
string类型
返回 String 包含的字符数
Varname
Variant类型
Len 会视其为 String 并且总是返回其包含的字符数
用户定义类型
返回其写至文件的大小
其它常规类型
存储变量所需的字节数
  
  
  
  
  
  
  
  
  
以上参数类型同样适用于数组元素的类型
  

Sub LenDemo()
    Dim arrV(1 To 2)
    Dim arr(1 To 2) As Long
    Dim v As Variant
    Dim l As Double
    arr(1) = 1234567
    v = arr(1): l = arr(1)
    arrV(1) = arr
    Debug.Print Len(arr(1))     ' arr(1)是Long类型
    Debug.Print Len(v)          ' v是Variant类型
    Debug.Print Len(arrV(1)(1)) ' arrV(1)(1)是Variant类型
    Debug.Print Len(l)          ' l是Long类型
End Sub

回复

使用道具 举报

发表于 2011-12-30 14:09 | 显示全部楼层
在Excel工作表中,Len()函数一律返回单元格内容作为字符串时的字符个数。

================

在Excel的VBA代码过程中,
Len()函数在对变量未作事先申明定义时,

首先判断其是否刚被定义数据类型:
如果明确了数据类型为数值型,
即整数、长整数、或单精度、双精度型数值,货币型
则首先返回其所占字节。

而对于文字变量或可变类型变量,则一律作为字符串,而返回其字符串个数。
  1. Sub test()
  2.     Dim a
  3.     Debug.Print a, Len(a), " Variant"
  4.    
  5.     Dim b As Integer
  6.     Debug.Print b, Len(b), "% Integer"
  7.    
  8.     Dim c As Long
  9.     Debug.Print c, Len(c), "& Long"
  10.    
  11.     Dim d As Single
  12.     Debug.Print d, Len(d), "! Single"
  13.    
  14.    
  15.     Dim e As Double
  16.     Debug.Print e, Len(e), "# Double"
  17.    
  18.     Dim f As Currency
  19.     Debug.Print f, Len(f), "@ Currency"
  20.    
  21.     Dim g As String
  22.     Debug.Print g, Len(g), "$ String"
  23.    
  24.     Dim h As Variant
  25.     Debug.Print h, Len(h), " Variant"
  26.    
  27.     Debug.Print "=========="
  28. End Sub
复制代码
代码运行结果为:

==========
            0    Variant
0          2  % Integer
0          4  & Long
0          4  ! Single
0          8  # Double
0          8  @ Currency
            0   $ String
            0    Variant
==========

其中,对于数值型数据,即整数、长整数、或单精度、双精度型数值,货币型,
会自动给予 0 值。

而对于字符串或可变变量,则仍为空值。=empty


回复

使用道具 举报

发表于 2011-12-30 14:27 | 显示全部楼层
为作对比,进一步修改代码如下:

即,去掉刚才sub test2 中的第一句(默认为可变变量)的变量声明语句
去掉! : Dim a, b, c, d, e, f, g, h : 去掉!
  1. Sub test3()
  2.    
  3.     ReDim a(0)
  4.     Debug.Print a(0), Len(a(0)), "a,  Variant"
  5.    
  6.     ReDim b(0) As Integer
  7.     Debug.Print b(0), Len(b(0)), "b, % Integer"
  8.    
  9.     ReDim c(0) As Long
  10.     Debug.Print c(0), Len(c(0)), "c, & Long"
  11.    
  12.     ReDim d(0) As Single
  13.     Debug.Print d(0), Len(d(0)), "d, ! Single"
  14.    
  15.    
  16.     ReDim e(0) As Double
  17.     Debug.Print e(0), Len(e(0)), "e, # Double"
  18.    
  19.     ReDim f(0) As Currency
  20.     Debug.Print f(0), Len(f(0)), "f, @ Currency"
  21.    
  22.     ReDim g(0) As String
  23.     Debug.Print g(0), Len(g(0)), "g, $ String"
  24.    
  25.     ReDim h(0) As Variant
  26.     Debug.Print h(0), Len(h(0)), "h,  Variant"
  27.    
  28.     Debug.Print "=========="
  29. End Sub


复制代码
运行结果,len的返回结果,又成为返回数值型变量所占字节数的结果啦。

==========
            0   a,  Variant
0          2  b, % Integer
0          4  c, & Long
0          4  d, ! Single
0          8  e, # Double
0          8  f, @ Currency
            0   g, $ String
            0   h,  Variant
==========
回复

使用道具 举报

发表于 2011-12-30 14:38 | 显示全部楼层    本楼为最佳答案   
本帖最后由 香川群子 于 2011-12-30 14:41 编辑

最后总结一下:

len函数返回变量的结果,按如下规律:

1. 如果变量为字符串变量,或variant可变变量,
则一律作为字符串处理而返回【变量所含内容】的字符个数,即字符长度。
如果变量为空白(empty)则返回结果=0

2. 如果变量一开始就被明确定义为数值型变量,
则len函数不论变量中所含内容如何,一律返回【变量所占】字符字节数。

分别为:
1,      byte
2 , % Integer
4 , & Long
4 , ! Single
8 , # Double
8 , @ Currency
8 , date

^^^^^^^^^

评分

参与人数 1 +6 收起 理由
Select + 6 学习这个小结

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2011-12-30 15:01 | 显示全部楼层
我不知这样评好吗,看高手们自由讨论{:181:}
回复

使用道具 举报

发表于 2011-12-30 16:30 | 显示全部楼层
Select 发表于 2011-12-30 15:01
我不知这样评好吗,看高手们自由讨论

看看看也
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 15:38 , Processed in 0.805309 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表