该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)
更多信息: