Excel VBA查找和替换

Excel VBA查找和替换,excel,vba,replace,find,Excel,Vba,Replace,Find,我有这个代码,它工作得很好。现在我需要改变它,我不知道怎么做 代码将在整个wordksheet中搜索。我只需要在1列中搜索 代码搜索整个单元格。我需要搜索牢房的左侧、中部或右侧 Sub ChgInfo() Dim WS As Worksheet Dim Search As String Dim Replacement As String Dim Prompt As String Dim Title As String Dim MatchCase As Boolean Promp

我有这个代码,它工作得很好。现在我需要改变它,我不知道怎么做

代码将在整个wordksheet中搜索。我只需要在1列中搜索

代码搜索整个单元格。我需要搜索牢房的左侧、中部或右侧

Sub ChgInfo() 

Dim WS As Worksheet 
Dim Search As String 
Dim Replacement As String 
Dim Prompt As String 
Dim Title As String 
Dim MatchCase As Boolean 

Prompt = "What is the original value you want to replace?" 
Title = "Search Value Input" 
Search = InputBox(Prompt, Title) 

Prompt = "What is the replacement value?" 
Title = "Search Value Input" 
Replacement = InputBox(Prompt, Title) 

For Each WS In Worksheets 
WS.Cells.Replace What:=Search, Replacement:=Replacement, _ 
LookAt:=xlPart, MatchCase:=False 
Next 

End Sub

这就是你要找的吗

下面的代码将在每张图纸的A列中查找值

Sub ChgInfo()

    Dim WS As Worksheet
    Dim Search As String
    Dim Replacement As String
    Dim Prompt As String
    Dim Title As String
    Dim MatchCase As Boolean

    Prompt = "What is the original value you want to replace?"
    Title = "Search Value Input"
    Search = InputBox(Prompt, Title)

    Prompt = "What is the replacement value?"
    Title = "Search Value Input"
    Replacement = InputBox(Prompt, Title)

    For Each WS In Worksheets
        WS.Columns(1).Replace What:=Search, Replacement:=Replacement, LookAt:=xlPart, MatchCase:=False
    Next

End Sub
更新的答案

Sub ChgInfo()

    Dim WS As Worksheet
    Dim Search As String
    Dim Replacement As String
    Dim Prompt As String
    Dim Title As String
    Dim MatchCase As Boolean
    Dim cell As Range
    Dim rngFind As Range
    Dim firstCell As String

    Prompt = "What is the original value you want to replace?"
    Title = "Search Value Input"
    Search = Trim(InputBox(Prompt, Title))

    Prompt = "What is the replacement value?"
    Title = "Search Value Input"
    Replacement = Trim(InputBox(Prompt, Title))

    For Each WS In Worksheets
        Set rngFind = WS.Columns(1).Find(What:=Search, LookIn:=xlValues, lookat:=xlPart)

        If Not rngFind Is Nothing Then firstCell = rngFind.Address

        Do While Not rngFind Is Nothing
            rngFind = Replacement & Mid(rngFind, 5, Len(rngFind))
            Set rngFind = WS.Columns(1).FindNext(After:=rngFind)
            If firstCell = rngFind.Address Then Exit Do
        Loop
    Next

End Sub

我用它来代替某处的东西。它很简单,但对我很好

   Sub test()
Dim x As String, y As String
y = InputBox("Replace what?")
x = InputBox("Replace to?")
    [m12:m20,I2,O7,P5,P6].Replace what:=y, replacement:=x
 End Sub   

“代码搜索整个单元格。我需要搜索单元格的左侧、中部或右侧。”?
WS.Columns(1)替换内容:=搜索、替换:=替换、查看:=xlPart、MatchCase:=False
似乎您的代码已经将替换部分单元格内容,而不仅仅是整个文本。除非你的意思是“左、中、右”谢谢Santosh,这是50%,现在我只需要在单元格的前4个左字符中找到。例如,che单元格内容是:united,我需要找到字符单位并替换为aaaa离开:AAAED-united@RobertoBahia谢谢你举个例子。给我几分钟好吗。其他示例单元格内容Santoschoolman在最后一个字符中搜索A并替换E,然后单元格内容=Santoschoolmen离开第一个A。@RobertoBahia是否可以循环?我不可能不循环地找到答案。