如何在Excel中使用VBA进行搜索和替换?

如何在Excel中使用VBA进行搜索和替换?,excel,vba,Excel,Vba,我是VBA Excel编程新手。考虑一个具有NXN值的Excel表。我的任务是从列中搜索名为“牙刷击球”的文本。一列由多个“牙刷”值组成 一旦在单元格A11中找到该值,则我需要将D11 ie对应D列中的文本更改为“BATTERY”。D11已经有一些文字了,我需要用“电池”替换这些文字 我的代码是 Sub replacement() Dim S As String Dim H As String S = "TOOTHBRUSH BATT" For i =

我是VBA Excel编程新手。考虑一个具有NXN值的Excel表。我的任务是从列中搜索名为“牙刷击球”的文本。一列由多个“牙刷”值组成

一旦在单元格A11中找到该值,则我需要将D11 ie对应D列中的文本更改为“BATTERY”。D11已经有一些文字了,我需要用“电池”替换这些文字

我的代码是

Sub replacement()

    Dim S  As String
    Dim H  As String

    S = "TOOTHBRUSH BATT"

    For i = 1 To Range("A1").End(xlDown).Row
        If Range("A" & i) = S Then
            Range("D" & i) = "BATTERY"
        End If
    Next i

End Sub
使用自动过滤器(以下代码未测试)

使用自动过滤器(以下代码未测试)


这与埃里克的答案相似

' Declare range  to set to the first cell we find
Dim find as Range
set find = Range("A:A").Cells.Find(What:="TOOTHBRUSH BATT")

' This is the cell Address (in case it keeps looping back to beginning)
Dim addy as string
if not find is nothing then addy = find.address

' If we've found a cell then Keep Do something with it
Do while not find is nothing
    find.Value = "BATTERY"


    ' Find the next Cell
    set find = Range("A:A").Cells.Find(What:="TOOTHBRUSH BATT", After:= find)
    ' If the next found cell is the first one then exit sub/function
    if find.address = addy then exit sub
Loop

这与埃里克的答案相似

' Declare range  to set to the first cell we find
Dim find as Range
set find = Range("A:A").Cells.Find(What:="TOOTHBRUSH BATT")

' This is the cell Address (in case it keeps looping back to beginning)
Dim addy as string
if not find is nothing then addy = find.address

' If we've found a cell then Keep Do something with it
Do while not find is nothing
    find.Value = "BATTERY"


    ' Find the next Cell
    set find = Range("A:A").Cells.Find(What:="TOOTHBRUSH BATT", After:= find)
    ' If the next found cell is the first one then exit sub/function
    if find.address = addy then exit sub
Loop

我不太了解vba,但如果这是一次性的事情,你就不需要vba了?!添加一个新列并将D列的所有值粘贴到该列中,然后用类似于
if(A1='TOOTBRUSH';'BATTERY',E1)
现有代码有什么问题?威廉姆斯:我的原始文件没有做任何更改,但如果我从牙刷拍开始复制某些数据,并将其粘贴到其他工作表中,请尝试使用信息性标题,以方便将来的读者,并确实省略“紧急”还有其他形式的乞讨——这里所有的问题都同等重要。我对vba了解不够,但如果这是一次性的事情,你就不需要vba了?!添加一个新列并将D列的所有值粘贴到该列中,然后用类似于
if(A1='TOOTBRUSH';'BATTERY',E1)
现有代码有什么问题?威廉姆斯:我的原始文件没有做任何更改,但如果我从牙刷拍开始复制某些数据,并将其粘贴到其他工作表中,请尝试使用信息性标题,以方便将来的读者,并确实省略“紧急”和其他形式的乞讨-所有问题在这里都同等重要。如果使用上述代码,它将只找到第一个值并进行更改和停止。我有许多牙刷击球值。在这种情况下,你应该尝试使用自动过滤器。虽然上面的代码可以通过添加一些循环逻辑来循环,但这不是一种快速的方法。如果使用上面的代码,它将只找到第一个值并进行更改和停止。我有许多牙刷BATT值。在这种情况下,您应该尝试使用自动过滤器。虽然上面的代码可以通过添加一些循环逻辑来进行循环,但这不是一种快速的方法
' Declare range  to set to the first cell we find
Dim find as Range
set find = Range("A:A").Cells.Find(What:="TOOTHBRUSH BATT")

' This is the cell Address (in case it keeps looping back to beginning)
Dim addy as string
if not find is nothing then addy = find.address

' If we've found a cell then Keep Do something with it
Do while not find is nothing
    find.Value = "BATTERY"


    ' Find the next Cell
    set find = Range("A:A").Cells.Find(What:="TOOTHBRUSH BATT", After:= find)
    ' If the next found cell is the first one then exit sub/function
    if find.address = addy then exit sub
Loop