【问题标题】:Excel VBA find and replaceExcel VBA 查找和替换
【发布时间】:2013-07-15 01:59:58
【问题描述】:

我有这段代码,它工作正常。现在我需要改变它,我不知道怎么做。

整个 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

【问题讨论】:

  • “代码搜索整个单元格。我需要搜索单元格的左侧或中间或右侧。”是什么意思?
  • WS.Columns(1).Replace What:=Search, Replacement:=Replacement, LookAt:=xlPart, MatchCase:=False 似乎您的代码已经替换了部分单元格内容,而不仅仅是整个文本。除非您所说的“左侧或中间或右侧”是指其他意思

标签: excel vba replace find


【解决方案1】:

这是你要找的吗?

以下代码将在每张纸的 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

【讨论】:

  • 谢谢你,Santosh,这是 50% 现在我只需要在单元格的前 4 个左侧字符中找到。
  • 例如 che 单元格内容是:united-united 我需要找到字符单元并替换为 aaaa 离开:aaaaed-united
  • @RobertoBahia 感谢您提供示例。给我几分钟
  • 好的。其他示例单元格内容 santoshcoolman 在最后一个字符中搜索 A 并替换 E,然后单元格内容 = santoscoolmEn 离开第一个 a。
  • @RobertoBahia 你可以循环播放吗?如果不循环,我无法找到答案。
【解决方案2】:

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

   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   

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 2018-04-11
    • 2018-01-18
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    相关资源
    最近更新 更多