How to Extract a Nine-Digit Number from Excel Cells Using VBA
Question details
The user needs a method to pull a standalone 9-digit number out of an Excel cell that contains a mix of text and numbers, without accidentally extracting 9 digits from a longer number sequence.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Parsing and cleaning mixed data formats to isolate a specific 9-digit ID, code, or number format using macro automation.
- Observed behavior
- The goal is to accurately identify and extract only the 9-digit sequence by utilizing regular expressions to respect word boundaries.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled macros in your spreadsheet software settings before adding VBA code.
Use a Custom VBA Function (UDF) with Regular Expressions
Create a User Defined Function using the VBScript.RegExp object to define strict word boundaries, ensuring only standalone 9-digit numbers are matched.
Regular expressions (RegEx) offer a powerful, rule-based approach to text extraction. By applying a pattern like \b\d{9}\b, the function will only locate exactly nine digits surrounded by non-word characters (like spaces or punctuation).
This prevents the macro from incorrectly extracting a 9-digit chunk out of a 10-digit phone number or a 12-digit serial code.
Press the Alt + F11 shortcut keys simultaneously to launch the Visual Basic for Applications (VBA) Editor.
In the top menu, click 'Insert' and select 'Module'. This will create a blank canvas for your custom VBA code.
Paste a VBA function that initializes CreateObject("VBScript.RegExp"). Set the .Global property to False and the .Pattern property to "\b\d{9}\b". Ensure the function returns the first match's value.
Close the VBA editor and go back to your spreadsheet. In an adjacent cell, type the new custom formula, such as =Extract9(A2), and press Enter.

Run Macros and VBA Seamlessly with WPS Spreadsheets
WPS Spreadsheets provides excellent support for VBA macros and custom functions, allowing you to use RegEx and complex data extraction formulas just as you would in Microsoft Excel.
- 1. Open WPS Spreadsheets: Launch WPS Office and open your dataset containing the mixed text.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click on 'VBA Editor'.
- 3. Insert Your Code: Add a new module and paste your VBScript.RegExp extraction code.
- 4. Extract the Data: Return to the grid and use your newly created custom formula to pull the 9-digit numbers instantly.

Frequently Asked Questions
What does the pattern \b\d{9}\b mean in regular expressions?
The '\b' tag represents a word boundary, which prevents the regex from matching numbers hidden inside longer strings. The '\d' stands for any digit, and '{9}' specifies that there must be exactly nine digits in sequence.
Why is my custom VBA formula returning a #NAME? error?
This error typically occurs if macros are disabled in your spreadsheet settings, or if the VBA code was placed in a worksheet module instead of a standard module. Double-check that your code is in a standard Module and that macros are enabled.
Can I modify this VBA code to extract a 5-digit zip code instead?
Yes. You simply need to adjust the numerical value in the regex pattern. Changing the pattern to "\b\d{5}\b" will tell the function to look for standalone 5-digit numbers instead of 9-digit ones.
How do I extract multiple 9-digit numbers from the same cell?
To extract all matches rather than just the first one, you must change the RegExp object's .Global property to True in your VBA code. You will then need to loop through the match collection and output them, usually separated by a comma or space.




