VBA & Macro Problems

Extract 10-Digit and 7-Digit Phone Numbers in Excel Using VBA

Steve KSteve K Nov 4, 2026 871 views

Question details

The user needs to extract both 10-digit and 7-digit phone numbers from mixed text strings in a single column and output the results into separate columns.

Extract 10-Digit and 7-Digit Phone Numbers in Excel Using VBA
Product
Excel
Device & OS
not provided
Scenario
Cleaning up inconsistent text data to isolate and extract valid phone numbers of different lengths using custom functions.
Observed behavior
Text strings contain mixed data, requiring pattern matching (Regular Expressions) via VBA to reliably extract 10-digit and 7-digit numbers without manual data entry.
Before you start

Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer options to access the VBA editor.

Solution 1Recommended

Create a VBA Custom Function Using Regular Expressions

Use a User-Defined Function (UDF) powered by VBScript.RegExp to identify and extract 7-digit phone numbers from text strings.

Regular expressions (RegEx) are highly effective at finding specific text patterns, such as phone numbers, hidden within chaotic data strings. By building a custom function, you can use RegEx seamlessly as a standard formula in your Excel sheets.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor. Alternatively, navigate to the Developer tab and click on 'Visual Basic'.

2
Insert a New Module

In the VBA Editor, click 'Insert' from the top menu and select 'Module'. This will create a blank script window where you can paste your custom function.

3
Paste the VBA Code

Copy and paste the following code into the module window: Public Function GetPhoneNumberSevenD(inputString As String) As String Dim regex As Object Dim matches As Object GetPhoneNumberSevenD = "" Set regex = CreateObject("VBScript.RegExp") regex.Pattern = "([0-9]{3})[-. ]?([0-9]{4})" regex.Global = True Set matches = regex.Execute(inputString) If matches.Count = 0 Then Exit Function GetPhoneNumberSevenD = regex.Replace(matches(0).Value, "$1$2") End Function

4
Save and Close the Editor

Save your workbook (ensuring it is an .xlsm file format) and close the VBA Editor to return to your standard Excel worksheet.

Create a VBA Custom Function Using Regular Expressions
Understanding the Pattern: The RegEx pattern "([0-9]{3})[-. ]?([0-9]{4})" searches for three digits, an optional separator (dash, period, or space), followed by four digits.
Advanced Data Processing

Extract Phone Numbers Using WPS Spreadsheet

WPS Spreadsheet offers full support for VBA macros and Regular Expressions, allowing you to seamlessly create custom user-defined functions to extract phone numbers from messy text data. You can perform complex data cleaning efficiently without needing advanced programming environments.

  1. 1. Open WPS Spreadsheet: Launch WPS Office, create or open your existing spreadsheet containing the raw text data.
  2. 2. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'Macros', then select 'Visual Basic Editor'.
  3. 3. Insert and Run the Code: Go to Insert > Module, paste the RegExp phone number extraction code, and save. You can now use the custom formula directly in your worksheet cells.
Fully compatible with Microsoft Excel Macro-Enabled Workbooks (.xlsm)Built-in VBA editor for running advanced Regular Expression scriptsLightweight architecture handles large datasets and heavy formulas smoothlyFamiliar UI making the transition from MS Office intuitive and seamless
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA RegEx function return a #NAME? error?

A #NAME? error occurs if Excel doesn't recognize the function. Ensure that your workbook has macros enabled, and that you have pasted the VBA code into a standard 'Module' rather than a worksheet object or 'ThisWorkbook'.

How do I handle phone numbers that contain extensions?

To handle extensions (e.g., 'x123' or 'ext 456'), you need to adjust the Regular Expression pattern in your VBA code to account for trailing letters and numbers, or use Excel's 'Text to Columns' tool to split the extensions into a separate column before running the extraction function.

Can I extract multiple phone numbers from a single cell?

Yes, but the provided VBA code uses 'matches(0)' which only extracts the first match found. To extract multiple numbers, you will need to modify the VBA code to loop through all objects in the 'matches' collection and concatenate them or output them as an array.

Are 10-digit phone numbers extracted using the same method?

Yes. A 10-digit extraction function uses the exact same VBScript.RegExp method but requires a slightly longer pattern, such as "([0-9]{3})[-. ]?([0-9]{3})[-. ]?([0-9]{4})", to account for the area code.