Excel has no built-in REGEXEXTRACT function in most desktop versions, so the practical way to extract text with a regular expression is to use a short VBA function (a User Defined Function) that wraps the Windows scripting regex engine, or to use Power Query with a custom column if you prefer to avoid macros. The VBA route works in Excel for Microsoft 365 on Windows and Mac (Mac uses a slightly different engine reference) and lets you call =RegexExtract(A2, "pattern") like any other formula. Below are the exact steps, plus the mistakes that usually break it.

What You'll Need

  • Excel for Microsoft 365, Excel 2021, or Excel 2019 (desktop). The steps below use the desktop app; the web version cannot run VBA macros.
  • A workbook saved as .xlsm (macro-enabled) if you want the function to persist after closing.
  • Basic familiarity with the Visual Basic Editor (Alt+F11 on Windows, Fn+Option+F11 on Mac).
  • A test column of text to extract from, so you can verify results before applying to the whole sheet.

Step-by-Step

  1. Save a copy first. Macros cannot be undone with Ctrl+Z once they change cells, so keep an untouched backup of the workbook.
  2. Open the Visual Basic Editor. On Windows press Alt+F11. On Mac press Fn+Option+F11 (or use Tools > Macro > Visual Basic Editor from the menu). Menu labels can differ slightly by version, device, and region.
  3. Insert a module. In the editor choose Insert > Module. A blank code window appears.
  4. Paste the function. Copy this into the module:
    Function RegexExtract(ByVal text As String, ByVal pattern As String) As String
        Dim re As Object
        Set re = CreateObject("VBScript.RegExp")
        re.Global = False
        re.IgnoreCase = True
        re.Pattern = pattern
        If re.Test(text) Then
            RegexExtract = re.Execute(text)(0).Value
        Else
            RegexExtract = ""
        End If
    End Function
    On Mac, CreateObject("VBScript.RegExp") is not available; replace that line with Set re = CreateObject("MacScript") is not a regex engine either, so the reliable cross-platform option is to use Power Query instead (see the note after these steps).
  5. Close the editor and save as .xlsm. Choose File > Save As and pick Excel Macro-Enabled Workbook (*.xlsm). Saving as .xlsx will strip the macro on the next open.
  6. Test the function. In a cell next to your data type =RegexExtract(A2, "\d{3}-\d{4}") to pull a phone-number pattern, or =RegexExtract(A2, "[A-Z]{2}\d{6}") for an order code. Press Enter. The first match is returned; if nothing matches, the cell is blank.
  7. Fill down. Drag the fill handle or double-click it to apply the formula to the rest of the column. Check a few rows manually against the source text.
  8. Convert to values if needed. Copy the results, then use Paste Special > Values to replace the formulas with static text before sharing the file with anyone who has macros disabled.

If macros are blocked: Excel may show a security warning bar. Only enable macros in a file you created or trust. If your organisation disables macros entirely, use Power Query: Data > Get Data > From Table/Range, then Add Column > Custom Column and use the Text.Select or Text.BetweenDelimiters functions. Power Query has no native regex function, so for true regex you would need a custom M function, which is more advanced than the VBA route.

Common Mistakes

  • Saving as .xlsx. The function disappears when the file is reopened because the module is not stored.
  • Forgetting to escape backslashes. In VBA string literals, \d must be written as "\d" inside the formula, and in the code window a pattern like \d{3} is fine, but a literal backslash needs doubling in some contexts. Test one cell before filling down.
  • Using Global = True when you only want the first match. That returns the first match only anyway in this function, but if you later change the code to return all matches, the output cell will only show one value unless you join them.
  • Assuming the function works in Excel for the web. It does not; the web app cannot run VBA.
  • Patterns that are too greedy. .* will swallow the whole line. Use a tighter character class such as [A-Z0-9]+.

Tips

  • Keep the regex pattern in its own cell and reference it, so you can change it without editing every formula.
  • Use IgnoreCase = False if case matters for codes or IDs.
  • Test patterns in a free online regex tester first, then paste into the formula. Remember VBA uses the VBScript engine, which lacks some modern regex features such as lookbehind.
  • Name the module (for example RegexTools) so you can find it later.
  • If you share the workbook, include a note that macros must be enabled, or convert results to values first.

Troubleshooting

  • #NAME? error: The function is not recognised. Check the module was inserted in the correct workbook, not in a personal macro workbook that is closed, and that the code has no red syntax lines.
  • Blank result when you expect a match: The pattern is wrong for the text. Copy one sample value into a regex tester and adjust. Also check for hidden spaces with TRIM.
  • Security warning blocks the macro: Save the file to a trusted location, or in Excel go to File > Options > Trust Center > Trust Center Settings > Macro Settings and confirm macros are not fully disabled. Do not lower security settings for files from unknown sources.
  • Function works on Windows but not Mac: The VBScript regex engine is Windows-only. Use Power Query or run the workbook on Windows.
  • File opens with a repair prompt: The module may be corrupted. Restore from your backup and re-paste the code carefully.

Frequently Asked Questions

Does Excel have a built-in regex function?

Not in the desktop versions covered here. Excel for Microsoft 365 has added some text functions, but a general REGEXEXTRACT is not available in all builds, so VBA or Power Query remains the reliable route.

Can I use regex in Excel for the web?

No. The web version cannot run VBA macros, and Power Query regex support is limited to custom M code.

Will the function work on a Mac?

The VBA code shown uses a Windows-only regex engine. On Mac, use Power Query or a Windows machine.

How do I extract all matches, not just the first?

Modify the function to loop through re.Execute(text) and join the results with a delimiter such as a comma, then return that string.

Is enabling macros safe?

Only enable macros in workbooks you created or received from a trusted source. Macros can run code on your computer, so treat unknown .xlsm files with caution.

Can I avoid macros entirely?

Yes, for simple patterns. Use Data > Text to Columns, LEFT/MID/FIND, or Power Query's Text.BetweenDelimiters. True regex without VBA requires a custom Power Query function.