VBA & Macro Problems

Extract Dollar Amounts from Text Using Excel VBA Regular Expressions

Olivia MillerOlivia Miller Nov 2, 2026 869 views

Question details

The user needs to extract monetary dollar amounts from strings using VBA, accounting for commas, decimals, spaces, and position within the text.

How to Extract Dollar Amounts from Text Using Excel VBA Regular Expressions
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press `Alt + F11` to launch the Microsoft Visual Basic for Applications editor in Excel.

2
Insert a New Module

Click `Insert` in the top menu bar, then select `Module` to create a blank scripting window.

3
Write the Regex Function

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.

4
Extract the Capture Group

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.

5
Apply in Your Worksheet

Return to your Excel sheet and type `=YourFunctionName(A1)` to extract the dollar amount from the target cell.

Use a Custom VBA Regular Expression Function
Regex Pattern Breakdown: This pattern ensures it looks for a start of a string or non-digit, finds a literal '$', ignores optional whitespace, and strictly extracts groups of numbers with optional comma separators and decimals.
Advanced Macro Support

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. 1. Download and Install WPS Office: Download the free suite and open WPS Spreadsheet.
  2. 2. Access the Developer Tools: Navigate to the Developer tab or press Alt+F11 to open the built-in VBA editor.
  3. 3. Insert Your Code: Paste your custom Regex or String extraction function into a new module.
  4. 4. Process Your Data: Return to your worksheet and apply your new formula to clean your financial data.
Fully compatible with Microsoft Excel VBA formats (.xlsm)Lightweight architecture ensures fast processing of complex regex operationsBuilt-in data cleaning and text-to-column capabilitiesFamiliar user interface requiring zero learning curve for Excel users
microsoft office alternative - wps office

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.