Find and replace If Then ?

  • Thread starter Thread starter Office_Novice
  • Start date Start date
O

Office_Novice

Here is what I haveCells.Replace What:="Insurance Company Insurance Policy #
RBH26374", Replacement:= _
"Insurance Policy - Insurance Company", LookAt:=xlPart,
SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

What i need is ...

If the words insurance company appears i need to replace them with
"Insurance Policy - Insurance Company" is this possible?
 
Here is what I haveCells.Replace What:="Insurance  Company Insurance Policy #
RBH26374", Replacement:= _
        "Insurance Policy - Insurance Company", LookAt:=xlPart,
SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

What i need is ...

If the words insurance company appears i need to replace them with  
"Insurance Policy - Insurance Company" is this possible?

Dim tRange as range
Sub ReplaceIf
set tRange=Range("A1:C10") ' Change to suit

For Each c in tRange
If c Like "*insurance company*" then
c.Value = "Insurance Policy - Insurance Company"
End If
Next
End Sub

Regards,

Per
 
If c Like "*insurance company*" then

Unless the user has an Option Compare Text statement in his/her code window,
your test above will be case sensitive. If you want to use the Like
operator, then you could try it this way...

If LCase$(c) Like "*insurance company*" Then

however, I'm thinking this might be better...

If InStr(1, c, "insurance company", vbTextCompare) Then

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top