简体   繁体   English

VBA 查找/替换和文本颜色更改

[英]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). A 列中有一些句子(用黑色字体书写)。 Columns C has a list of terms to search for. C 列有一个要搜索的术语列表。 And column D has a list of replace terms (written in red font color). D 列有一个替换术语列表(以红色字体颜色书写)。

I'm trying to search the sentences in column A for the search terms in column C.我正在尝试在 A 列中的句子中搜索 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.并且,如果 A 列包含 C 列中的任何搜索词,请将 A 列中的文本替换为 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.但我无法让 A 列中字符串的替换部分的字体颜色变为红色。

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.然后在对 Range object 的 Replace 方法的后续调用中使用替换标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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