Extract Dollar Amounts from Text Using Excel VBA Regular Expressions
Question details
The user needs to extract monetary dollar amounts from strings using VBA, accounting for commas, decimals, spaces, and position within the text.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Cleaning or processing financial text data to isolate exact dollar values from mixed text descriptions.
- Observed behavior
- Financial amounts are embedded within varying text strings, requiring a reliable method to parse and capture the numerical values regardless of their position.
If you plan to use early binding for regular expressions in VBA, ensure you have enabled the 'Microsoft VBScript Regular Expressions 5.5' library via Tools > References in the VBA Editor.
Use a Custom VBA Regular Expression Function
Implement a precise regular expression pattern in VBA to locate and extract dollar amounts, cleanly handling optional spaces, commas, and decimals.
A highly reliable method for extracting numeric values formatted as currency within text strings is to use a specific Regular Expression (Regex). The pattern `(?:^|\D)\$\s*([0-9]+(?:,[0-9]{3})*(?:\.[0-9]+)?)(?=\D|$)` effectively ignores letters and selectively captures the digits following a dollar sign.
Press `Alt + F11` to launch the Microsoft Visual Basic for Applications editor in Excel.
Click `Insert` in the top menu bar, then select `Module` to create a blank scripting window.
Create a User-Defined Function (UDF) using the `CreateObject("VBScript.RegExp")` method. Assign the pattern `(?:^|\D)\$\s*([0-9]+(?:,[0-9]{3})*(?:\.[0-9]+)?)(?=\D|$)` to the `.Pattern` property.
In your function, execute the match and return `Match.SubMatches(0)` so that only the numeric amount (Capture Group 1) is extracted, discarding the dollar sign itself.
Return to your Excel sheet and type `=YourFunctionName(A1)` to extract the dollar amount from the target cell.

Extract Values Using Built-in VBA String Functions
If you prefer not to use regular expressions, a custom VBA function using InStr and Mid can locate the dollar sign and extract the subsequent number.
Use Excel Flash Fill (No VBA Required)
For straightforward and consistent data patterns, Excel's native Flash Fill feature can automatically extract the dollar amounts without requiring any programming knowledge.
Extract Text and Data Efficiently with WPS Spreadsheet
WPS Spreadsheet offers powerful data handling capabilities, including full compatibility with standard VBA macros. Executing custom regular expression functions and automating complex financial data extraction is fast and seamless.
- 1. Download and Install WPS Office: Download the free suite and open WPS Spreadsheet.
- 2. Access the Developer Tools: Navigate to the Developer tab or press Alt+F11 to open the built-in VBA editor.
- 3. Insert Your Code: Paste your custom Regex or String extraction function into a new module.
- 4. Process Your Data: Return to your worksheet and apply your new formula to clean your financial data.

Frequently Asked Questions
Why am I getting a 'User-defined type not defined' error when using Regex in VBA?
This error occurs when you use early binding (e.g., `Dim regEx As RegExp`) without enabling the correct library reference. To fix this, go to Tools > References in the VBA Editor and check 'Microsoft VBScript Regular Expressions 5.5'. Alternatively, use late binding by declaring the object as Object and using `CreateObject("VBScript.RegExp")`.
Can I extract multiple dollar amounts from a single cell using VBA?
Yes. If your cell contains multiple dollar amounts, you need to set the `Global` property of your RegExp object to `True` (e.g., `regEx.Global = True`). Your function will then need a loop to iterate through the `MatchCollection` and return all matched sub-items.
Does Flash Fill update automatically if the original text changes?
No, Flash Fill performs a static extraction at the moment you run it. If your source data updates frequently, utilizing a custom VBA function or complex built-in text formulas is a better solution, as they will recalculate automatically.




