如何在单元格中查找值并将其删除?

我想在范围内find一个值,然后删除该值。

我find了一些代码,它查找的值,但删除其他细胞,而不是find的价值。

这是代码:

Private Sub CLEAROLD_Click() 'PURPOSE: Clear out all cells that do not contain a specific word/phrase Dim Rng As Range Dim cell As Range Dim ContainWord As String 'What range do you want to search? Set Rng = Range("AA2:AC25") 'sub for the word shorttext = traintype1.Value & number1.Value 'What phrase do you want to test for? ContainWord = shorttext 'Loop through each cell in range and test cell contents For Each cell In Rng.Cells If cell.Find(ContainWord) Is Nothing Then cell.ClearContents Next cell End Sub 

我试图改变if条件,但我不能让它工作。

由于您使用For Each cell In Rng.Cells中循环遍历Range的单元格,因此可以直接使用ContainWord比较单元格的值。

更换:

 If cell.Find(ContainWord) Is Nothing Then cell.ClearContents 

附:

 If cell.Value2 = ContainWord Then cell.ClearContents 

使用replacefunction不会更快吗?

  rng.Replace What:=shorttext, Replacement:="", LookAt:=xlWhole, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False