Removing Cells Containing Specific Terms

by Allen Wyatt
(last updated July 3, 2021)

Ankur has a worksheet that has thousands of cells containing various terms. He needs to delete a large number of those terms and the cells in which they occur. For instance, he may need to delete all the cells containing terms like "google," "youtube," "linkedin," and numerous other terms. Ankur knows he can do a search and replace for each of these terms, but that is quite tedious. He wonders if there is a way he can identify all the terms to be removed and then have Excel remove them from those thousands of cells.

If you need to do this type of thing on a regular basis, then the best solution is to use a macro. The trick is how you specify, in the macro, exactly what you want to replace. One way is to stuff the information into an array, as in the following example:

Sub RemoveTerms1()
    Dim vTerm As Variant
    Dim vArray As Variant

    vArray = Array("google", "youtube", "linkedin")
    For Each vTerm in vArray
        Selection.Replace What:=vTerm, _
          Replacement:="", LookAt:=xlPart
    Next vTerm
End Sub

The terms being looked for are placed in the vArray array, and then each member of the array (each term) is stepped through. The Replace method is used with the Selection object to do the actual replacements. The macro doesn't remove cells; it simply removes any text that matches the term. When replacing, the searching is case insensitive, so that "google" matches "Google."

Since the Selection object is used, it is important to make sure you select the list you want to process before actually running the macro. If you don't, then nothing is replaced.

If you prefer, you could create a macro that pulled the terms from a range of cells in the workbook.

Sub RemoveTerms2()
    Dim c As Range
    Dim rngSource As Range
    Dim vTerm As Variant
    Dim arrTerms As Variant
    Dim i As Integer

    i = -1
    arrTerms = Array()
    For Each c In Range("D1:D9").Cells
        If Trim(c.Value) > "" Then
            i = i + 1
            ReDim Preserve arrTerms(i)
            arrTerms(i) = Trim(c.Value)
        End If
    Next c

    On Error Resume Next
    Set rngSource = Application.InputBox( _
                    Prompt:="Please select Range", _
                    Title:="Removing Cells Containing Terms", _
                    Default:=ActiveSheet.UsedRange.Address, Type:=8)
    On Error GoTo 0
    If rngSource Is Nothing Then
        MsgBox ("You didn't specify a range to process")
    Else
        For Each vTerm in arrTerms
            rngSource.Replace What:=vTerm, _
              Replacement:="", LookAt:=xlWhole
        Next vTerm
    End If
End Sub

This macro pulls the search terms from the range D1:D9 and then prompts you to choose the range of cells you want to process. It uses the same Replace method that was used in the previous macro, except it specifies the LookAt parameter to be xlWhole. This means that the search term needs to match the entire cell in order to be removed. The terms are still considered case insensitive, though.

Note that the examples so far don't actually delete any cells; they simply delete contents of cells. In many cases this is exactly what you want because you don't want to disrupt the layout of the actual worksheet. If you really do want to delete cells, then you wouldn't use the Replace method. Instead, you could turn on expanded text comparison and use the Like operator to see if there is a match.

Option Compare Text

Sub RemoveTerms3()
    Dim c As Range
    Dim rngSource As Range
    Dim vTerm As Variant
    Dim arrTerms As Variant
    Dim i As Integer
    Dim sLook As String

    i = -1
    arrTerms = Array()
    For Each c In Range("D1:D9").Cells
        If Trim(c.Value) > "" Then
            i = i + 1
            ReDim Preserve arrTerms(i)
            arrTerms(i) = Trim(c.Value)
        End If
    Next c

    On Error Resume Next
    Set rngSource = Application.InputBox( _
                    Prompt:="Please select Range", _
                    Title:="Removing Cells Containing Terms", _
                    Default:=ActiveSheet.UsedRange.Address, Type:=8)
    On Error GoTo 0
    If rngSource Is Nothing Then
        MsgBox ("You didn't specify a range to process")
    Else
        For Each vTerm in arrTerms
            sLook = "*" & vTerm & "*"
            For Each c In rngSource
                If c.Value Like sLook Then c.Delete
            Next
        Next vTerm
    End If
End Sub

Note that the search terms are still pulled from the D1:D9 range and you are still asked for the range you want to process. From there, though, the process is different: The macro examines each cell and if there is a partial match, then the cell is deleted.

In order for this variation on the macro to work properly, you'll need to include the Option Compare Text line outside of the procedure itself. This instructs VBA to enable the keywords (such as Like) that allow comparing text.

Note:

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 (13373) 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. ...

MORE FROM ALLEN

Only Inline Figures Can be Seen and Printed

Insert a graphic into a document and you expect to be able to see it. What do you do if it isn't displayed, however? Here ...

Discover More

Word Count for Headers and Footers

Getting a word count for an entire document is rather easy. If you want a word count for a special part of a document ...

Discover More

Copying and Moving Footnotes and Endnotes

If you need to move footnotes or endnotes from one location to another in a document, you can use editing techniques you ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Searching for Line Breaks

If you need to find where line breaks are located in cells, there are a couple of ways you can proceed. Here's a quick ...

Discover More

Changing Default Search Settings

Excel provides some great tools for finding information in a worksheet or a workbook. Changing the default settings used ...

Discover More

Finding All Instances of a Value

Searching for information in an Excel worksheet generally goes very smoothly. There can be times, however, when the ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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}] 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 9 + 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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
Subscribe

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.