Extract numbers from text
2 min readJul 4, 2021
The formula will extract numbers from text:
=IF(SUM(LEN(B3)-LEN(SUBSTITUTE(B3, {"0","1","2","3","4","5","6","7","8","9"}, "")))>0, SUMPRODUCT(MID(0&B3, LARGE(INDEX(ISNUMBER(--MID(B3,ROW(INDIRECT("$1:$"&LEN(B3))),1))* ROW(INDIRECT("$1:$"&LEN(B3))),0), ROW(INDIRECT("$1:$"&LEN(B3))))+1,1) * 10^ROW(INDIRECT("$1:$"&LEN(B3)))/10),"")
LAMBDA:
=LAMBDA(text_value,IF(SUM(LEN(text_value)-LEN(SUBSTITUTE(text_value, {"0","1","2","3","4","5","6","7","8","9"}, "")))>0, SUMPRODUCT(MID(0&text_value, LARGE(INDEX(ISNUMBER(--MID(text_value,ROW(INDIRECT("$1:$"&LEN(text_value))),1))* ROW(INDIRECT("$1:$"&LEN(text_value))),0), ROW(INDIRECT("$1:$"&LEN(text_value))))+1,1) * 10^ROW(INDIRECT("$1:$"&LEN(text_value)))/10),""))
VBA Code:
Function Extract_Number_VBA(rcell As Range, _
Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As Double
Dim iCount As Integer, i As Integer, iLoop As Integer
Dim sText As String, strNeg As String, strDec As String
Dim lNum As String
Dim vVal, vVal2
sText = rcell
If Take_decimal = True And Take_negative = True Then
strNeg = "-" 'Negative Sign MUST be before 1st number.
strDec = "."ElseIf Take_decimal = True And Take_negative = False Then
strNeg = vbNullString
strDec = "."ElseIf Take_decimal = False And Take_negative = True Then
strNeg = "-"
strDec = vbNullString
End IfiLoop = Len(sText)
For iCount = iLoop To 1 Step -1
vVal = Mid(sText, iCount, 1)If IsNumeric(vVal) Or vVal = strNeg Or vVal = strDec Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
If IsNumeric(lNum) Then
If CDbl(lNum) < 0 Then Exit For
Else
lNum = Replace(lNum, Left(lNum, 1), "", , 1)
End If
End If
If i = 1 And lNum <> vbNullString Then lNum = CDbl(Mid(lNum, 1, 1))
Next iCountExtract_Number_VBA = CDbl(lNum)
End Function
GitHub:
https://github.com/havishmad/excel_extract_numbers
YouTube:
https://youtu.be/c5mbx926iK4