如何在Excel VBA中找到一个单词,并使用VBA代码将它涂成红色(只有单词而不是整个单元格)?

时间:2021-08-01 22:17:43

I am writing a code in Excel, a part of which needs to find a word in a cell (not case sensitive) and color it red (only the word not the entire cell).
For example;

我正在用Excel编写代码,其中一部分需要在单元格中找到一个单词(不区分大小写),并将它涂成红色(只有单词而不是整个单元格)。例如;

Cell Text - What is your name? Searching text - IS

手机短信-你叫什么名字?搜索文本,

I need to color 'is' red in the cell text.

我需要在单元格文本中显示“is”的颜色。

1 个解决方案

#1


1  

Theodora, The following has the basic components of what you're after. You will, of course, need to modify it to fit your specific needs:

西奥多拉,以下是你所追求的基本要素。当然,您需要修改它以满足您的特定需求:

Sub ColorPart()
    searchString = " is "
    pos = InStr(Cells(1, 1), searchString)
    If pos > 0 Then
        Cells(1, 1).Characters(Start:=pos, Length:=Len(searchString)).Font.color = vbRed
    End If
End Sub

Hope this helps. Additionally, in fairness to @Peh, although my answer was not based on his comment, it does essentially solidify what he had already correctly laid out.

希望这个有帮助。此外,公平地说,@Peh,尽管我的回答不是基于他的评论,但它实际上巩固了他已经正确地阐述的内容。

#1


1  

Theodora, The following has the basic components of what you're after. You will, of course, need to modify it to fit your specific needs:

西奥多拉,以下是你所追求的基本要素。当然,您需要修改它以满足您的特定需求:

Sub ColorPart()
    searchString = " is "
    pos = InStr(Cells(1, 1), searchString)
    If pos > 0 Then
        Cells(1, 1).Characters(Start:=pos, Length:=Len(searchString)).Font.color = vbRed
    End If
End Sub

Hope this helps. Additionally, in fairness to @Peh, although my answer was not based on his comment, it does essentially solidify what he had already correctly laid out.

希望这个有帮助。此外,公平地说,@Peh,尽管我的回答不是基于他的评论,但它实际上巩固了他已经正确地阐述的内容。