Deleting All Characters Up Through a Unique Character Sequence

by Allen Wyatt
(last updated February 27, 2021)


Gary has a worksheet that contains a lot of information pasted from other sources. Sometimes cells will contain unwanted characters, and deleting all the unwanted characters cell by cell can be tedious. Gary can tell that all the unwanted characters always end in, say, a sequence such as "x27g," though the number of characters before this sequence can vary from cell to cell. He wonders if there a way to use Find and Replace to delete not only the sequence but everything before the sequence from all the cells in a worksheet.

At first blush, this may seem like the type of task that is best suited to a macro. This is not the case, however. You can actually accomplish the task using Find and Replace with wildcards. Testing shows that this will work just fine:

  1. Press Ctrl+H. Excel displays the Replace tab of the Find and Replace dialog box. (See Figure 1.)
  2. Figure 1. The Replace tab of the Find and Replace dialog box.

  3. In the Find What box, enter an asterisk followed by your ending sequence. Thus, in Gary's case, he would enter "*x27g" (without the quote marks).
  4. Make sure the Replace With box is empty.
  5. Click Replace All.

If that doesn't work for you, it is possible that there are some of the Find and Replace options set incorrectly. You can display the Replace tab of the Find and Replace dialog box again, but this time click the Options button. This results in an expanded dialog box. Make sure that no formatting is set, that the Look In drop-down list is set to Formulas, and that the Match Entire Cell Contents check box is cleared.

If, for some reason, you prefer to use a macro to do the replacements, the following will work just fine.

Sub DeleteBefore()
    Dim rCell As Range
    Dim sFind As String
    Dim iLen As Integer
    Dim iFind As Integer

    sFind = "x27g" 'Change as desired
    iLen = Len(sFind)
    For Each rCell In Selection
       iFind = InStr(rCell, sFind)
       If iFind > 0 Then
           rCell.Value = Mid(rCell, iFind + iLen)
       End If
End Sub

This is a simple macro that looks at each cell in whatever range you've selected. If the cell contains your ending sequence (specified in the sFind variable), then everything up through that sequence is deleted.


If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13828) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Indent and Justify Command

WordPerfect users are familiar with the F4 command, which indents and justifies a paragraph. Word does not have an ...

Discover More

Repeating Rows at the Bottom of a Page

Excel allows you to repeat rows at the top of every page of a printout. If you want to repeat rows at the bottom of every ...

Discover More

Automatic Row Height For Merged Cells with Text Wrap

When you have text wrap turned on in a cell, Excel expands the height of the row as you add more text to the cell. When ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Removing Cells Containing Specific Terms

Macros are great for processing large amounts of data quickly. This tip examines several ways you can remove specific ...

Discover More

Finding Boolean Values

Excel worksheets can contain all sorts of data. One thing you might store in a worksheet is a range of Boolean (TRUE or ...

Discover More

Limitations On Finding Characters

When you search for information in a worksheet, you expect Excel to return results that make sense. If you don't get a ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 4?

2021-03-01 06:49:44

Mike D.

I was thinking the macro method as well. I started to work on a solution for this but life has a funny way of getting in the way.
I keep forgetting that we can use wild cards in the Find Box.
I guess I look for the complicated solution first and then look for the easy way out. Must be the engineer in me.

2021-02-28 02:40:42


Find and replace all option does'nt work well. I cross checked the options formatting ,that is set correctly, still.

This Site

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.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.