LEN()在VBA中返回错误的值

Rau*_*les 3 excel vba excel-vba

我有这个简单的数据集:

230
16000
230
230000
230000
230000
16000000
230000
230000
Run Code Online (Sandbox Code Playgroud)

我想要的是得到每个单元格的长度但是当我写这段代码时:

Sub LengthOfCell()
Dim c As Long
Dim result As Integer

c = ActiveCell.Value
result = Len(c)
Debug.Print (result)


End Sub
Run Code Online (Sandbox Code Playgroud)

对于第一个单元格(230),它给出2,对于任何超过3位数的数字,它应该是3和4.不知道我做错了什么.tis仅用于更大SUB的概念证明:

Public Sub SortMyData()

'approach: convert line to string and concatenate to that as it's a lot less picky than Excel's formats, then replace cell value with the new string.
'          Excel will then define the string type as either Percentage or Scientific depending on the magnitude.
Dim i As Integer
Dim N_Values As Integer

N_Values = Cells(Rows.Count, 2).End(xlUp).Row
            'Range("B6", Range("B5").End(xlDown)).Count

For i = 6 To N_Values 'iteration loop from 6 (first row of value) to N_Values (last filled row)
    Cells(i, 3).NumberFormat = "0"

    If Cells(i, 2).NumberFormat <> "0.0%" Then
        Cells(i, 2).NumberFormat = "0.0%"
        Cells(i, 2).Value = Cells(i, 2).Value / 100

        ElseIf Len(Cells(i, 3).Value > 3) Then
            Cells(i, 3).Value = Cells(i, 3).Value / 1000

        ElseIf Cells(i, 3).Value = Null Then
            Cells(i, 3).Value = 0

    Else
        Cells(i, 2).Value
        Cells(i, 3).Value
    End If
       ' If Len(Cells(i, 3) > 3) Then
           ' Cells(i, 3).Value = Cells(i, 3).Value / 1000
           ' ElseIf Cells(i, 3).Value = Null Then
                'Cells(1, 3).Value = 0
       ' Else
           ' Debug.Print
       ' End If
Next

 End Sub
Run Code Online (Sandbox Code Playgroud)

use*_*332 5

Len是一个String类型的函数

@Shai Rado,在新手的答案中要小心这些陈述......

F1:Len函数
返回一个Long,其中包含字符串中的字符数或存储变量所需的字节数.


Sla*_*lai 5

关闭)是在错误的地方.

If Len(Cells(i, 3).Value > 3) Then
Run Code Online (Sandbox Code Playgroud)

应该

If Len(Cells(i, 3).Value) > 3 Then
Run Code Online (Sandbox Code Playgroud)

Len(Cells(i, 3).Value > 3)将评估为Len("True")Len("False"),因此它将始终为True(任何非零数字为True)