Link to home
Start Free TrialLog in
Avatar of samprg
samprg

asked on

Macro Replace

Hello,

I have many like ="<a href='"&CURL("http://Google.com">"Google"</a>"
in many sheets .
I need a macro to search and  replace "Google" with other value get it from below, in all sheets
Sheet name Cell       Value
MT                 C5       Bing.com
Thanks
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

so u need to change google.com to bing.com and what about the "Google" change it to "Bing" ?
If I understood correctly you need to loop thru all the sheets and replace above but what is
Sheet name Cell       Value
MT                 C5       Bing.com
?
gowflow
Avatar of samprg
samprg

ASKER

If I need to change Bing.com to other one , just I will put the new value in C5 and run the macro


Sheet name Cell       Value
MT                 C5       Bing.com
Avatar of samprg

ASKER

Sorry,
the macro should get the new value from sheet MT C5
and you need to change only in sheet MT ?
gowflow
Avatar of samprg

ASKER

You  got it
fine you say you have many sheets that contains the info you want to change and you say that what you want to change is in Cell C5 then you confirm that you need to change only sheet MT isn't it redundant ??
gowflow
Avatar of samprg

ASKER

The macro takes a new value from sheet MT only, so it isn't redundant.
ok fine do you have these instances also in sheet MT ???
that was my question

Anyway the below code will change all instances of "Google" to anything found in Cell C5 of sheet MT. It will not look for these instances in sheet MT !! If you need it also to look for these instances in sheet MT let me know I will fix it

gowflow
Sub ChangeStr()
Dim WS As Worksheet, Wst As Worksheet
Dim Cell As Range
Dim ReplaceBy As String
Dim FirstAddress As String
Dim I As Long

Set WS = Sheets("MT")
ReplaceBy = WS.Range("C5")

For Each Wst In ActiveWorkbook.Worksheets
    If Wst.Name <> WS.Name Then
        Set Cell = Wst.Find("Google", LookIn:=xlValues, lookat:=xlPart)
        If Not Cell Is Nothing Then
            FirstAddress = Cell.Address
            Do
                Cell = Replace(Cell, "Google", ReplaceBy)
                I = I + 1
                Set Cell = Wst.FindNext(Cell)
            Loop While Not Cell Is Nothing And Cell.Address <> FirstAddress
    End If
    
Next Wst

 
MsgBox ("Total of " & I & " instances of 'Google' where found and replaced by '" & ReplaceBy & "' successfully.")
     
End Sub

Open in new window

Avatar of samprg

ASKER

I got error. see the screenshot
And please I need the macro takes the old value from C6. Thanks
Error.JPG
Oops revised code
gowflow
Sub ChangeStr()
Dim WS As Worksheet, Wst As Worksheet
Dim Cell As Range
Dim ReplaceBy As String
Dim FirstAddress As String
Dim I As Long

Set WS = Sheets("MT")
ReplaceBy = WS.Range("C5")

For Each Wst In ActiveWorkbook.Worksheets
    If Wst.Name <> WS.Name Then
        Set Cell = Wst.Find("Google", LookIn:=xlValues, lookat:=xlPart)
        If Not Cell Is Nothing Then
            FirstAddress = Cell.Address
            Do
                Cell = Replace(Cell, "Google", ReplaceBy)
                I = I + 1
                Set Cell = Wst.FindNext(Cell)
            Loop While Not Cell Is Nothing And Cell.Address <> FirstAddress
        End If
    End If
    
Next Wst

 
MsgBox ("Total of " & I & " instances of 'Google' where found and replaced by '" & ReplaceBy & "' successfully.")
     
End Sub

Open in new window

Do not use the second version just chking something our replies crossed.
gowflow
Avatar of samprg

ASKER

Gives me same error .
I think the error in " Set Cell = Wst.Find("Google", LookIn:=xlValues, lookat:=xlPart) "
ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samprg

ASKER

Awesom
Your welcome and sorry it took more than it should !!! sometimes I get clumzy !
Tks for the grade !
Cheers
gowflow
Avatar of samprg

ASKER

That's OK
Avatar of samprg

ASKER

I need you