How to get the text between two dashes with TEXTBEFORE
When a reference packs several parts into one cell, like INV-1001-A, you often want just the piece before the first dash. This pulls it out.
The formula
=TEXTBEFORE(TEXTAFTER(A2,"-"),"-") in column B
What each part does
TEXTAFTER(A2,"-")Everything after the first dash. This still includes the last part of the code.
TEXTBEFORE(...,"-")From that, the part before the next dash, which is the middle.
How it works, and what to watch for
TEXTBEFORE stops at the first dash and hands you everything in front of it. Point it at the last dash instead and it keeps more.
If the dash is ever missing, the modern version returns #N/A. Wrapping it in IFERROR lets you fall back to the whole cell gracefully.
On older Excel this becomes LEFT paired with FIND, which is the version the settings switch will give you.
Questions
What if some cells have no dash?
Wrap it in IFERROR so those fall back to the whole value: =IFERROR(TEXTBEFORE(A2,"-"),A2).
How do I get the part after the dash?
Use TEXTAFTER the same way, which keeps everything past the dash instead of before it.
The example sheet
Reference
Middle
INV-1042-B
CRN-2210-A
ORD-8899-C
If this helped, these are close by
More on TEXTBEFORE , and every walkthrough that uses it.
Formula List
Formula Fixer
Formula List
Settings: separator, Excel version
Argument separator
Set by your computer's language, not by the file. A frequent cause of a correct-looking formula being rejected.
Comma =SUM(A1,B1)
Semicolon =SUM(A1;B1)
Excel version
XLOOKUP, IFS, TEXTBEFORE and TEXTJOIN need Microsoft 365 or 2021; older versions return #NAME? instead. Choosing the older one switches every walkthrough to a formula that works there.
Microsoft 365 or 2021
2019 or earlier
Is a formula returning an error?
Paste it below for a line-by-line check.
Check the formula
Clear
or press Enter
Try one of these
© Formula Steps — formulasteps.com
Bookmark
Share