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

Vba 如何在单元格中查找值并将其删除?,vba,excel,Vba,Excel,我想在某个范围内找到一个值,然后删除该值 我找到了一些代码,它查找值,但随后删除了其他单元格,而不是找到的值 代码如下: 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

我想在某个范围内找到一个值,然后删除该值

我找到了一些代码,它查找值,但随后删除了其他单元格,而不是找到的值

代码如下:

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
条件,但无法使其正常工作。

由于您对Rng中的每个单元格都使用
。单元格
循环遍历范围的单元格,因此可以直接将单元格的值与
ContainWord
进行比较

替换:

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


由于您对Rng.Cells中的每个单元格使用
循环遍历范围单元格,因此可以直接将单元格值与
ContainWord
进行比较

替换:

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


使用替换功能不是更快吗

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

使用替换功能不是更快吗

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

THX现在可以工作了。所以问题是,我没有为(if)rightshx使用新的值空间,因为它现在可以工作了。所以问题是我没有为(如果)权利使用新的价值空间你做了什么改变?(不起作用的改动)问题的标题不必只用大写。你做了什么改动?(没有用的改动)没有必要在问题的标题中只使用大写字母。