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

时间:2016-10-06 10:48:48

标签: excel vba excel-vba

我有这个简单的数据集:

230
16000
230
230000
230000
230000
16000000
230000
230000

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

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

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


End Sub

对于第一个单元格(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

5 个答案:

答案 0 :(得分:5)

  

Len是字符串类型函数

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

  

F1:Len功能
  返回包含a中字符数的长   string 或存储变量所需的字节数

答案 1 :(得分:5)

结束)位置错误。

If Len(Cells(i, 3).Value > 3) Then

应该是

If Len(Cells(i, 3).Value) > 3 Then

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

答案 2 :(得分:2)

由于您正在查找单元格中的字符数(数字),因此需要更改为Sub LengthOfCell() Dim c As String Dim i As Long Dim result As Integer For i = 1 To 9 c = CStr(Cells(i, 1).Value) result = Len(c) Debug.Print result Next i End Sub 并稍微修改一下代码,它会为您提供所需的结果。

见下面的短子:

FileReader

答案 3 :(得分:2)

不确定为什么你要包括Dim c As Long件 - 为什么不试试这个:

Sub LengthOfCell()
Dim result As Integer

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


End Sub

这对我来说很好..

答案 4 :(得分:2)

值和显示文本之间似乎存在混淆。 Range().Text将返回范围原始值,其中,Cstr(Range().Value)Sub Demo() Dim r As Range For Each r In Range("A2:A9") r.Value = 230 r.Offset(0, 1) = r.NumberFormat r.Offset(0, 2) = Len(r.Value) r.Offset(0, 3) = Len(r.Text) r.Offset(0, 4) = Len(CStr(r.Value)) Next End Sub 将返回格式化值。

enter image description here

private void setUpAndLoadNativeExpressAds() {
    // Use a Runnable to ensure that the RecyclerView has been laid out before setting the
    // ad size for the Native Express ad. This allows us to set the Native Express ad's
    // width to match the full width of the RecyclerView.
    mRecyler.post(new Runnable() {
        @Override
        public void run() {
            final float density = getActivity().getResources().getDisplayMetrics().density;
            // Set the ad size and ad unit ID for each Native Express ad in the items list.

            //Here is the problem :
            for (int i = 0; i <= randomQuote.size(); i += ITEMS_PER_AD) {
                final NativeExpressAdView adView =
                        (NativeExpressAdView) randomQuote.get(i);
                AdSize adSize = new AdSize(
                        (int) (mRecyler.getWidth() / density),
                        NATIVE_EXPRESS_AD_HEIGHT);
                adView.setAdSize(adSize);
                adView.setAdUnitId(AD_UNIT_ID);
            }

            // Load the first Native Express ad in the items list.
            loadNativeExpressAd(0);
        }
    });
}