Len函数返回不正确的结果

时间:2014-11-30 11:57:25

标签: excel vba excel-vba

我对LEN功能有一些奇怪的问题。我的输入是字符串" ......" LEN返回长度为2.我不知道为什么会发生这种情况。有什么建议? 我使用的代码是:

Function replaceCharWith(ByVal str As String, ByVal chToReplace As String, ByVal ch As String) As String
For i = 1 To Len(str)

    If Mid(str, i, 1) = chToReplace Then
        replaceCharWith = replaceCharWith & ch
    Else
        replaceCharWith = replaceCharWith & Mid(str, i, 1)
    End If
Next
End Function

Sub dotsToSlashes()
    Dim c As Range
    For Each c In Selection
        c = replaceCharWith(c, ".", "/")
    Next
End Sub

感谢。

1 个答案:

答案 0 :(得分:6)

我怀疑你的字符串不是“......”而是“......”。这是HORIZONTAL ELLIPSIS Unicode x2026的2倍。如果自动更正选项是默认值,Excel将自动替换... ...

参见示例:

Sub HorizontalEllipsesTest()

 s = "......"
 MsgBox Len(s)

 s = ChrW(8230) & ChrW(8230) 'x2026 = 8230
 MsgBox s
 MsgBox Len(s)

End Sub

自动更正选项在哪里? https://support.office.com/en-us/article/Where-are-the-AutoCorrect-options-cb889db0-07f8-400a-b6ef-4192cc05cbc3