Copy/paste these two functions to a general module in your workbook.
Function RemAlphas(ByVal sStr As String) As Long
Dim i As Long
If sStr Like "*[0-9]*" Then
For i = 1 To Len(sStr)
If Mid(sStr, i, 1) Like "[0-9]" Then
RemAlphas = RemAlphas & Mid(sStr, i, 1)
End If
Next i
Else
RemAlphas = sStr
End If
End Function
Function RemDigits(str As String) As String
'Remove numbers from a string Ron Rosenfield Mar 20, 08
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\d+"
RemDigits = re.Replace(str, "")
End Function
In A1 enter =remalphas(D1)
In B1 enter =remdigits(D1)
Copy down as far as you need.
Gord Dibben MS Excel MVP
Hi Mike.. Ran the code and got "Compile Error" Variable not defined for
lastrow = Cells etc.... Please advise thanks Patt
This isn't any faster than your RemAlpha routine (probably slower) but for
consistency with the RemDigits function:
It probably should be called: RemNonDigits, though.
================================
Function RemAlpha(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\D"
RemAlpha = re.Replace(str, "")
End Function
===============================
Or you could make it more generalized to Remove whatever is specified in the
Pattern:
==============================
Function RemSpec(str As String, sPattern As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPattern
RemSpec = re.Replace(str, "")
End Function
===============================
So, for sPattern (what will be removed)
"\d" = digits
"\D" = non-digits
"[A-Za-z]" = all letters
and there are many other patterns that could be used.
--ron