Written by Allen Wyatt (last updated March 9, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
Steven has a worksheet that has lots of text in it. In the cells in column A he wants to delete everything that may occur before a given sequence of characters, such as everything before =XX=. There may be multiple instances of these characters in each cell, but Steven only wants to delete everything before the first occurrence.
One way to do this is to use a formula. If you are using Excel 2021 or the version included with Microsoft 365, then you can use the following formula:
=TEXTAFTER(A1,"=XX=")
If you have some cells that may not include your delimiters, then you need to compensate for them. This version of the formula uses the IFERROR function to handle what to do if the delimiter is not there:
=IFERROR(TEXTAFTER(A1,"=XX="),A1)
If you are using an older version of Excel, then you need to rely on a formula that is a bit more complex. For instance, the following formula will evaluate whatever is in cell A1 and simply return everything up to the =XX= characters. If the characters are not found in the cell, then the entire cell is returned:
=RIGHT(A1,IF(ISERROR(FIND("=XX=",A1,1)), LEN(A1),LEN(A1)-FIND("=XX=",A1,1)+1))
If you want, instead, to not return the first occurrence of =XX=, all you need to do is change the +1 near the end of the formula to -3.
If you prefer a macro-based solution you could use a routine like the following. It examines all the cells that are currently selected and then deletes everything before the =XX= sequence.
Sub DeleteToSequence() Dim rCell As Range Dim sSeq As String Dim x As Long sSeq = "=XX=" For Each rCell In Selection x = InStr(rCell.Value, sSeq) If x > 0 Then rCell.Value = Mid(rCell, x) End If Next Set rCell = Nothing End Sub
You should be aware that this macro can cause some errors, particularly when what you are searching for begins with an equal sign (as in =XX=). When a string beginning with an equal sign is stuffed back into the cell, you'll get a #NAME? error because Excel tries to parse the cell as if it contains a formula.
If you want to delete everything up through the character sequence, use this If structure in the middle of the routine instead of the one that is in the code above:
If x > 0 Then rCell.Value = Mid(rCell, x + Len(sSeq)) End If
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8446) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Deleting Everything Up to a Character Sequence.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Need to enter the current time into a cell? It's easy to do using this keyboard shortcut. The shortcut is a handy one to ...
Discover MoreAfter months or years of naming things (such as cell ranges), you may find your workbook cluttered with a bunch of names ...
Discover MoreUsing the AutoFill feature of Excel is very handy. If you want to expand the utility offered by the feature, all you need ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-03-10 12:09:43
J. Woolley
My Excel Toolbox includes the following function that is useful for manipulating the text described in this Tip:
=Between(Text, BeginAfter, EndBefore, [CaseSensitive], [Direction])
This function is like a combination of TEXTBEFORE and TEXTAFTER.
The following formulas produce the same results as the Tip's first two formulas but do not require Excel 2021+:
=Between(A1, "=XX=", "")
=IFERROR(Between(A1, "=XX=", ""), A1)
See https://sites.google.com/view/MyExcelToolbox/
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments