Excel like a finance pro.
Clean messy text with a pattern
Pull an invoice number out of free text when nested SUBSTITUTEs have gone four deep.
1What it does
Some text can't be cleaned by position or by a fixed replacement: a memo that says "payment for inv 1042 thanks", or "INV-1042", or "Invoice #1042" depending on who typed it. A regular expression describes the SHAPE of what you want — the letters I-N-V, maybe a dash or a space or a hash, then digits — and finds it wherever it is and however it's written. VBA reaches regular expressions through the VBScript library, created late-bound so it runs on any Windows machine without a reference. It's powerful and unforgiving: a pattern that's slightly too loose matches things it shouldn't.
2The code
Function ExtractInvoice(memo As String) As String
' Late-bound: no library reference needed.
Dim re As Object, m As Object
Set re = CreateObject("VBScript.RegExp")
' "inv" or "invoice", then optional space / dash / #, then 3-6 digits.
re.Pattern = "\binv(?:oice)?[\s#-]*(\d{3,6})\b"
re.IgnoreCase = True
re.Global = False
If re.Test(memo) Then
Set m = re.Execute(memo)(0)
ExtractInvoice = "INV-" & m.SubMatches(0) ' normalised form
Else
ExtractInvoice = ""
End If
End Function
Sub NormaliseMemos()
Dim r As Long, lastRow As Long
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
For r = 2 To lastRow
ActiveSheet.Cells(r, 2).Value = ExtractInvoice(CStr(ActiveSheet.Cells(r, 1).Value))
Next r
End SubPaste into the Visual Basic Editor (Alt + F11 → Insert → Module), then run and save as macro-enabled (.xlsm).
New to macros? Set up in 5 minutes▾
- 1
Don't see the Developer tab in the ribbon?
You don't strictly need it — Alt + F11 opens the editor directly — but it makes running macros easier.- Windows: File → Options → Customize Ribbon → tick Developer in the right-hand list → OK.
- Mac: Excel → Preferences → Ribbon & Toolbar → tick Developer → Save.
- 2
Paste in the code
Press Alt + F11 to open the Visual Basic editor, then Insert → Module and paste the snippet's code into the blank window. Close it with Alt + Q. - 3
Run it
Press Alt + F8, pick the macro's name, and click Run — that's it. (Pasted a custom function instead? Just type it into a cell like any built-in:=GrossMargin(B2, B3).) - 4
Keep the macro — save as .xlsm
File → Save As → Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx silently drops the code when you save. - 5
Macros blocked?
Click Enable Content on the yellow bar. If you downloaded the file, you may first need to right-click it → Properties → tick Unblock → OK, then reopen.
Heads up: macros can't be undone with Ctrl + Z — save a copy before running one that changes your workbook.
3When you use it
- Pull an invoice number out of a free-text bank memo, however it was typed.
- Strip everything except digits from a reference field.
- Validate that a code matches the expected shape before importing it.
4See it in action
A simulation — press Run to perform what the macro does to a sample workbook.
| Memo | Extracted |
|---|---|
| payment for inv 1042 thanks | INV-1042 |
| INV-1043 | INV-1043 |
| Invoice #1044 - Acme | INV-1044 |
| call back on 5551234 | — |
| ref 2026-08 transfer | — |
Three differently-typed invoice references found and normalised; the phone number and the date left alone, because the pattern requires the prefix.
5Pitfalls
The pattern is too loose and matches a phone number or a date as an invoice.
Fix: Anchor it with word boundaries (\b) and a required prefix, and test it against real memos before trusting it.
CreateObject("VBScript.RegExp") fails on a Mac.
Fix: VBScript is Windows-only. On Mac, use Like patterns or string functions instead.
Only the first match is found when there are several.
Fix: Set re.Global = True and loop the Matches collection.
Backslashes in the pattern are doubled or missing.
Fix: In VBA source a backslash needs no escaping: write \d as it appears in the pattern, once.
6No-code alternatives
- The Like operator — Built into VBA with simpler wildcards — enough when the shape is fixed and you only need a yes/no.
- SUBSTITUTE, FIND and MID — Fine for one or two known formats. A pattern earns its place once the variations multiply.
Rather not write macros? Wauvel's free tools generate branded, formula-driven Excel for you — no VBA required.
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.