提取excel范围内的唯一字符,但某些字符功能除外

时间:2017-06-25 15:58:40

标签: excel excel-vba excel-formula vba

如果excel在A1:B100中有字符串数据,我想提取除A-Z a-z 0-1以外的字符,并且每个字符显示一次。我想得+, - 等等 有人可以帮我一把吗?

示例数据

+------------+------+
|    ColA    | ColB |
+------------+------+
| Blue Berry | 4    |
| Apple      | 4+   |
| Orange;    | 44   |
+------------+------+

我希望结果在一个单元格中作为Space +;

1 个答案:

答案 0 :(得分:2)

试试这个:

Sub showspecials()
    Dim r As Range, rng As Range, msg As String
    Dim S As String, L As String, CH As String
    Set rng = Range("A1:B100")
    msg = ""

    For Each r In rng
        S = r.Text
        L = Len(S)
        If L <> 0 Then
            For i = 1 To L
                If Mid(S, i, 1) Like "[0-9a-zA-Z]" Then
                Else
                    msg = msg & vbCrLf & Mid(S, i, 1)
                End If
            Next i
        End If
    Next r

        MsgBox msg
End Sub

enter image description here

如果您希望角色在MsgBox中仅显示一次,请使用:

Sub showspecials()
    Dim r As Range, rng As Range, msg As String
    Dim S As String, L As String, CH As String
    Dim i As Long, pat As String

    Set rng = Range("A1:B100")
    msg = ""
    pat = "[0-9a-zA-Z]"

    For Each r In rng
        S = r.Text
        L = Len(S)
        If L <> 0 Then
            For i = 1 To L
                CH = Mid(S, i, 1)
                If CH Like pat Then
                Else
                    If InStr(msg, CH) = 0 Then
                        msg = msg & vbCrLf & CH
                    End If
                End If
            Next i
        End If
    Next r

        MsgBox msg
End Sub