Jared
Jared

Reputation: 25

VBA Find/Replace & Text Color Change

I've a spreadsheet with three columns. Column A has some sentences in it (written in black font color). Columns C has a list of terms to search for. And column D has a list of replace terms (written in red font color).

I'm trying to search the sentences in column A for the search terms in column C. And, if column A contains any of the search terms in column C, replace the text in column A with the replace terms in column D.

The find/replace feature works great. But I haven't been able to get the font color of the replaced portion of the string in column A to turn red.

Any thoughts?

Here's the code I've got so far.

Private Sub CommandButton1_Click()

For i = 3 To 6

     Worksheets("Sheet1").Range("A2:A35").Select

     Selection.Replace What:=Cells(i, 3).Value, Replacement:=Cells(i, 4).Value, _
     LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False

Next

Worksheets("Sheet1").Cells(1, 1).Select

End Sub

Upvotes: 0

Views: 4374

Answers (1)

Andre Gamez
Andre Gamez

Reputation: 33

Try this:

Private Sub CommandButton1_Click()

For i = 3 To 6

     Worksheets("Sheet1").Range("A2:A35").Select

     With Application.ReplaceFormat
       .Font.Color = vbRed
     End With

     Selection.Replace What:=Cells(i, 3).Value, Replacement:=Cells(i, 4).Value, _
     LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, ReplaceFormat:=True

Next

Worksheets("Sheet1").Cells(1, 1).Select

End Sub

Microsoft Explanation:

Sets the replacement criteria to use in replacing cell formats. The replacement criteria is then used in a subsequent call to the Replace method of the Range object.

Upvotes: 0

Related Questions