How to extract numbers from a string in Excel
How to extract numbers from a string in Excel: You have been given a table of customer information and asked to extract only the numerical parts (phone numbers, customer IDs, ID card numbers, birth year, etc.) to a separate file for transactions or communication. How do you extract numbers from a large data table? Follow the steps below to learn how to do it.
For example, suppose we have a data table as shown in the image below. We need to extract the ID card number of each person. Since the data is not consistent, we cannot use basic character extraction functions like RIGHT, MID, LEFT. Follow the steps below to extract the numbers.
Step 1
First, use the Alt + F11 shortcut to open the Microsoft Visual Basic for Applications window. Then select the Insert tab on the toolbar. The scroll bar appears, select the Module option.
Step 2
Next, select and copy the entire code below by right-clicking and selecting Copy or using the Ctrl + C shortcut.
Function ExtractNumber(rCell As Range)
Dim lCount As Long
Dim sText As String
Dim lNum As String
sText = rCell
For lCount = Len(sText) To 1 Step -1
If IsNumeric(Mid(sText, lCount, 1)) Then
lNum = Mid(sText, lCount, 1) & lNum
End If
Next lCount
ExtractNumber = CLng(lNum)
End Function
Step 3
Go back to the Module window that was just opened, right-click, and select Paste or use the Ctrl + V shortcut to paste the copied code here.
Step 4
That’s it, the ExtractNumber function has been added to your Excel. Now, return to the worksheet and use the following formula:
=ExtractNumber(B2)
Copy the formula for the other cells below to quickly extract the ID card numbers of each person.
In this way, the above article has instructed you on how to extract numbers from a string in Excel. Hope the article is useful for you in your work. Good luck!