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.
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.
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!
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
Excel provides some great tools for finding information in a worksheet or a workbook. Changing the default settings used ...Discover More
Searching for information in an Excel worksheet generally goes very smoothly. There can be times, however, when the ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.