Segregate Alphanumeric Characters from a String of Mixed Characters Inside an Excel Cell using VBA code

115 Views Asked by At

I segregate numeric characters from a string of mixed characters inside a cell.

Function xxx(str As String) As String
Dim strarr() As String
str = Replace(str, ".", " ")
strarr = Split(str)
Dim i As Long
For i = 0 To UBound(strarr)
    If strarr(i) Like "[A-Za-z0-9]" Then
        numfromstring = numfromstring & "," & strarr(i)
    End If
Next i

numfromstring = Mid(numfromstring, 2)
End Function

How can I do the same for alphanumeric characters?

1

There are 1 best solutions below

2
VBasic2008 On

Get Alpha-Numeric UDF

Function GetAlphaNumeric( _
    ByVal Str As String, _
    Optional ByVal Delimiter As String = ",") _
As String
    
    Dim n As Long, Char As String, rStr As String
    
    For n = 1 To Len(Str)
        Char = Mid(Str, n, 1)
        If Char Like "[A-Za-z0-9]" Then rStr = rStr & Delimiter & Char
    Next n
    
    If Len(rStr) > 0 Then GetAlphaNumeric = Mid(rStr, 1 + Len(Delimiter))
    
End Function