0

我正在尝试从带有格式化文本的文本框中删除回车:

For i = ThisShape.TextFrame2.TextRange.Runs.Count To 1 Step -1
  ThisText = ThisShape.TextFrame2.TextRange.Runs(i).Text
  ThisText = Replace(ThisText, Chr(13), "")
  ThisShape.TextFrame2.TextRange.Runs(i).Text = ThisText
Next i

出于某种奇怪的原因,文本框仍然带有回车符。任何帮助,将不胜感激。

大卫

4

1 回答 1

0

CLEAN功能很少使用,因此经常被遗忘。

CLEAN从给定文本中删除不可打印的字符(ASCII 0 到 31)。您可以在 VBA 中使用它:

myStr = Application.WorksheetFunction.Clean(myStr)

...或作为 Excel 工作表函数:

=CLEAN(A1)

例子:

Sub cleanDemo()

    Dim mystr As String

    'populate string with linefeeds and carriage returns.
    'note: vbLf = chr(10): vbCr = chr(13) : vbCrLf = vbCr+vbLf
    mystr = "A" & vbLf & "B" & vbCr & "C" & vbCrLf & "D"

    'display string and it's length
    MsgBox mystr, , "myStr is " & Len(mystr) & " char long."

    'remove ASCII 0 through 31
    mystr = Application.WorksheetFunction.Clean(mystr)

    'display string and it's length
    MsgBox mystr, , "myStr is " & Len(mystr) & " char long."

End Sub

...返回:

图像


列出字符串中的所有字符:

该过程列出了在指定字符串中找到的字符及其代码:

Sub showAllChars(strIn as String)
'Lists all ASCII character codes used in the specified string.
'Adaptable to UNICODE by changing Asc to AscW and Chr to ChrW
Dim strIn As String, c As String, p As Long

For p = 1 To Len(strIn)
    c = Mid(strIn, p, 1)
    Debug.Print "Pos#" & p & ": Chr(" & Asc(c) & ") =[";
    Select Case Asc(c)
        Case 10
            Debug.Print "LF]", ;
        Case 13
            Debug.Print "CR]", ;
        Case Else
            Debug.Print c & "]", ;
    End Select
    If p / 5 = p \ 5 Then Debug.Print
Next p
Debug.Print "Done. (Length=" & Len(strIn) & ")"
End Sub

示例输出:

Pos#1: Chr(65) =[A]         Pos#2: Chr(66) =[B]         Pos#3: Chr(13) =[CR]        
Pos#4: Chr(67) =[C]         Pos#5: Chr(63) =[?]         Pos#6: Chr(68) =[D]         
Pos#7: Chr(17) =[]          Pos#8: Chr(69) =[E]         Pos#9: Chr(70) =[F]         
Pos#10: Chr(63) =[?]        Pos#11: Chr(71) =[G]        Pos#12: Chr(10) =[LF]       
Pos#13: Chr(72) =[H]        Pos#14: Chr(73) =[I]        Done. (Length=14)

更多信息:

于 2018-05-19T21:24:36.877 回答