How to find string and replace it?

iand5

New Member
Joined
Jul 26, 2017
Messages
36
I already have the code for replacing a string with a different string. It works when I know what the contents of the cell are. For example, in this case "A1" contains the string "test".
Code:
Sub Button1_Click()

Dim OriginalText As String
Dim CorrectedText As String


OriginalText = Range("A1").Value
CorrectedText = Replace(OriginalText, "test", "exam")


Range("A1").Value = CorrectedText


End Sub


I am trying to figure out how to replace a string when I don't know the contents of the cell.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
If you don't know the contents of the cell, how do you know if it needs replacing ???
 
Upvote 0
In the case I don't know the cell number where the string I'm trying to replace is located.
 
Upvote 0
So you want to search the entire sheet or entire workbook for What and replace it with what.

For example look for Duck and replace it with Cat.
We need information like this.
 
Upvote 0
.
I'm with Michael M .. if you don't know the cell address and you don't know the term you are seeking to replace,
how else is there to pinpoint what you are replacing ?
 
Upvote 0
Assuming say range A1 to A10....something like this will do the trick, but I'm guessing there will be a much shorter version available

Code:
Sub Button1_Click()
Dim r As Long
For r = 1 To 10
    If InStr(Range("A" & r).Value, "test") Then
        Range("A" & r).Value = Replace(Range("A" & r).Value, "test", "Exam")
    End If
Next r
End Sub
 
Upvote 0
So you want to search the entire sheet or entire workbook for What and replace it with what.

For example look for Duck and replace it with Cat.
We need information like this.

Sorry, it's been a long day heh.

Yes, that's pretty much that's what I am trying to do.

Search the entire sheet. Look for a string "Duck"(for example) and replace it with "Cat".
 
Upvote 0
So would you be looking for:

The Duck is Yellow.
And replace Duck with

The flower is Yellow

Or are you looking for just Duck in a cell and replace Duck with Flower.

Have you looked into the built in function in Excel.
On the ribbon you should see Find and inside Find you have a replace section.
 
Upvote 0
.
Code:
Option Explicit


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)
      
    Set WS = ThisWorkbook.ActiveSheet
   
        WS.Cells.Replace What:=Search, Replacement:=Replacement, _
        LookAt:=xlPart, MatchCase:=False
    
     
End Sub
 
Upvote 0
.
Code:
Option Explicit


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)
      
    Set WS = ThisWorkbook.ActiveSheet
   
        WS.Cells.Replace What:=Search, Replacement:=Replacement, _
        LookAt:=xlPart, MatchCase:=False
    
     
End Sub

Thanks!

What if I already know what string I want to replace and its replacement string?
Just search the entire worksheet without having to prompt?
 
Upvote 0

Forum statistics

Threads
1,224,924
Messages
6,181,787
Members
453,066
Latest member
Firemonte

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top