View Single Post
  #19   Report Post  
Posted to microsoft.public.excel.misc
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default separating alpha numeric vlue

I suggest to take
=regexpreplace($A1,"(\D*)(\d+)(\D*)","$1$3")
for the text part and
=regexpreplace($A1,"(\D*)(\d+)(\D*)","$2")
for the number part.

See http://www.sulprobil.com/html/regexp.html.


While it was not part of the OP's indicated requirements, your suggested
method will fail to return the correct results if there are any embedded
digits within the text portion of the string of text passed into it (for
example, A1 containing AB12CD5678) or if the string of text has digits on
both sides of the text. However, I do note that, for the OP's stated
requirement, your suggested solution will, in fact, properly handle the
digits on either the right or left hand side of the text; so it is
definitely a valid solution for the OP's stated needs. My own personal
preference though, if I were going to use a macro function solution instead
of the spreadsheet formula solution I posted earlier, would be to use a more
straight-forward VBA function that does not make use of regular expressions
in order to get the text part...

Function GetTextPart(SourceString As String) As String
Dim X As Long
For X = 1 To Len(SourceString)
If Not IsNumeric(Mid(SourceString, X, 1)) Then
GetTextPart = Mid(SourceString, X)
Exit For
End If
Next
For X = Len(GetTextPart) To 1 Step -1
If Not IsNumeric(Mid(GetTextPart, X, 1)) Then
GetTextPart = Left(GetTextPart, X)
Exit For
End If
Next
End Function

and then use a simple SUBSTITUTE spreadsheet function to get the digits
part. Again, that is a personal preference given I find regular expressions
somewhat hard to construct or to read back later on.

Rick