仅打印字符串的字母

时间:2017-03-06 08:50:53

标签: excel excel-vba vba

我有一个字符串,我想从中检查每个元素是alphabetnumber a"。"或者" /"并只打印字符串的字母?

1 个答案:

答案 0 :(得分:1)

这两个函数的相当不优雅的组合可以做到:

Function strip_non_alpha_words(sentence As String) As String
Dim wrd_to_check As String
For Each wrd In Split(sentence, " ")
    wrd_to_check = wrd
    If wrd_to_check = alpha_only(wrd_to_check) Then
        strip_non_alpha_words = strip_non_alpha_words & wrd_to_check & " "
    End If
Next
strip_non_alpha_words = Trim(strip_non_alpha_words)
End Function

Function alpha_only(mixedStr As String) As String
Dim ltr As Long, ascii_code As Long
For ltr = 1 To Len(mixedStr)
    ascii_code = Asc(UCase(Mid(mixedStr, ltr, 1)))
    If (ascii_code > 64 And ascii_code <= 90) Then
        alpha_only = alpha_only & Mid(mixedStr, ltr, 1)
    End If
Next
End Function

您可以在公式(UDF)中使用它,如下所示:

=strip_non_alpha_words(A1)