Formula to Parse numeric text in single field into multiple columns - Excel

clintnash
clintnash used Ask the Experts™
on
I have a field coming from a database that contains a string of data like so:
H1:M4:L5
H4:M3:L8

I need to split this into three different columns but only the numeric value
Col1             Col2            Col3
1                     4                  5
4                     3                  8

I have tried about 10 different approaches and am getting results, just not usable results.

Any advise would be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if field is always the same format you can do
=MID(A1,2,1)
=MID(A1,5,1)
=MID(A1,8,1)

Each one in diff cell
byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013

Commented:
I wrote a user-defined function (UDF) to split your text and return the numeric values. You use store the UDF in a regular module sheet (just like a macro). As written, the UDF doesn't care how many segments there are coming over from the database. Nor does it care how many letter precede the numbers, or how many digits there are in each number.

Below the code, I show two different ways of using the UDF in worksheet formulas. The sample workbook attached to this Comment shows those formulas in action.
Function SplitNumbers(sText As String, sDelimiter As String) As Variant
Dim v As Variant, vResults As Variant, vSplit As Variant
Dim s As String
Dim i As Long
vSplit = Split(sText, sDelimiter)
ReDim vResults(0 To UBound(vSplit))
For Each v In vSplit
    s = StrReverse(v)
    vResults(i) = Val(StrReverse(Val(s) & ""))
    i = i + 1
Next
SplitNumbers = vResults
End Function

Open in new window

If you want to return all three values at once, select three cells in a row, then array-enter a formula like:
=SplitNumbers(A1,":")

If you want to return the values individually, copy across a formula like:
=INDEX(SplitNumbers($A1,":"),COLUMNS($G1:G1))
GetNumbersQ28929030.xlsm
Mechanical Engineer
Most Valuable Expert 2013
Top Expert 2013
Commented:
If you really want a formula only solution, I suggest the following:
1. Use the Data...Text to Columns method with a colon as the delimiter to split your data into 3 columns.
2. Use a formula like shown below to extract the numeric portion out of up to a 5 character string in cell K1:
=VALUE(CONCATENATE(IFERROR(--MID(K1,1,1),""),IFERROR(--MID(K1,2,1),""),IFERROR(--MID(K1,3,1),""),IFERROR(--MID(K1,4,1),""),IFERROR(--MID(K1,5,1),"")))

If you need more possible characters, then keep adding IFERROR blocks to the concatenation.

I've updated the sample workbook to show the formula method in action. I also included some formulas to split the text into three substrings.
GetNumbersQ28929030.xlsm
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Another UDF.
Assuming your string is in A1, then try this....
In B1
=ExtractNumber($A1,COLUMNS($B1:B1))
and then copy across to three columns.

Pls refer to the attached for more details.
Function ExtractNumber(Str As Range, i As Long) As String
Dim RegEx As Object
Dim Match As Object
Set RegEx = CreateObject("vbscript.regexp")
With RegEx
   .IgnoreCase = True
   .Pattern = "\d"
   .Global = True
End With
If RegEx.test(Str) = True Then
   Set Match = RegEx.Execute(Str)
   ExtractNumber = Match(i - 1)
Else
   ExtractNumber = ""
End If
End Function

Open in new window

ExtractNumbers.xlsm
byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013

Commented:
I noticed that my UDF would truncate trailing zeros from the number returned. The formula only solution does not have such a problem.

The fix for the UDF (assuming your database text does not include any decimal points) is shown below. Leading zeros will still be truncated because the result is returned as numbers rather than text.
Function SplitNumbers(sText As String, sDelimiter As String) As Variant
Dim vResults As Variant, vSplit As Variant
Dim i As Long
vSplit = Split(sText, sDelimiter)
ReDim vResults(0 To UBound(vSplit))
For i = 0 To UBound(vSplit)
    vResults(i) = Val(StrReverse(CStr(Val("." & StrReverse(vSplit(i))))))
Next
SplitNumbers = vResults
End Function

Open in new window

Author

Commented:
Thank you for both options you provided. That one had stumped me...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial