简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM