简体   繁体   English

使用Excel VBA根据字符串中的单词更改单词的颜色?

[英]Using Excel VBA to change the colour of a word based on the word in a string?

I want the font colour to reflect the colour written as a word. 我希望字体颜色能够反映出写成单词的颜色。 eg Every time the word "red" appears in a string I want the font of the word red to be red (or highlighted in red). 例如,每次单词“red”出现在字符串中时,我希望单词red的字体为红色(或以红色突出显示)。 I have strings of text in cells with the name of a site, a deadline and RAG status. 我在单元格中有文本字符串,其中包含网站名称,截止日期和RAG状态。 These are within one cell, separated by a line break (char(10)). 它们在一个单元格内,由换行符(char(10))分隔。 I have columns of cells based on deadline date, and rows by work type so I can't easily split each text segment into its own cell and use conditional formatting without breaking this tabular layout. 我有基于截止日期的单元格列和按工作类型划分的行,因此我无法轻松地将每个文本段拆分为自己的单元格,并使用条件格式而不会破坏此表格布局。 The string is built from code which concatenates text, and then referenced in formula. 该字符串是根据连接文本的代码构建的,然后在公式中引用。 I can write basic VBA but haven't a clue how i could do this but have attached the concat code (from Chandoo) to illustrate how the text string is built up. 我可以编写基本的VBA,但是我不知道如何做到这一点但是附加了concat代码(来自Chandoo)来说明如何构建文本字符串。

Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
For Each cell In useThis
If CStr(cell.Value) <> "" And CStr(cell.Value) <> " " Then
retVal = retVal & CStr(cell.Value) & dlm
End If
Next
If dlm <> "" Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function

Can anybody advise how I should approach this please? 有人可以建议我该如何处理这个问题吗? Or suggest any alternatives to this approach. 或建议任何替代方法。

First, you need to find the start location of the search term within the string, so 首先,您需要在字符串中找到搜索词的起始位置,所以

startRed = InStr(0,searchstring,"Red",CompareMethod.Text)

then, within the specified cell, use the characters property and the known length to change the color 然后,在指定的单元格内,使用characters属性和已知长度来更改颜色

With Cell.Characters(Start:= startRed, Length:= Len("Red")).Font
     .Color = RGB(255,0,0)

Do this for each desired color and your cells will be changed as needed 对每种所需颜色执行此操作,您的单元格将根据需要进行更改

Thanks RGA. 谢谢RGA。 I used what you put to write the below. 我用你所写的内容写下面的内容。 Not the neatest but it allows me to colour each linebreak on my sheet with the corresponding colour to the text.I had to convert my formula to values for it to work. 不是最新的,但它允许我为我的工作表上的每个换行颜色与文本的相应颜色。我必须将我的公式转换为值才能工作。 Thanks again, I wouldn't have had a clue where to start without you. 再次感谢,我不知道在没有你的情况下从哪里开始。

Sub ColourText2()

TurnOff
Dim startRed As Integer, startChar As Integer, startAmber As Integer, startGreen As Integer, x As Integer, i As Integer, startLB As Integer, endLB As     Integer, iCount As Integer
Dim searchString As String, searchChar As String
Dim clr As Long
Dim cell As Range


For x = 6 To 22
iCount = Worksheets("MySheet").Range("D" & x & ":S" & x).Count

Range("C" & x).Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C" & x & ":S" & x), Type:=xlFillDefault
Range("C" & x & ":S" & x).Select
Worksheets("MySheet").Calculate
 Range("D" & x & ":S" & x).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

For Each cell In Worksheets("MySheet").Range("D" & x & ":S" & x)
searchString = cell


Application.StatusBar = i & "of: " & iCount
startChar = 1
    For startLB = 1 To Len(cell)

cell.Select
        If startChar = 1 Then
            startLB = 1
            endLB = 1
        Else
            startLB = InStr(endLB, searchString, Chr(10), vbTextCompare)
        End If

       startGreen = InStr(endLB, searchString, "green", vbTextCompare)
            'MsgBox startGreen
       startAmber = InStr(endLB, searchString, "amber", vbTextCompare)
          'MsgBox startAmber
       startRed = InStr(endLB, searchString, "red", vbTextCompare)
           'MsgBox startRed
       endLB = InStr(endLB + 1, searchString, Chr(10), vbTextCompare)

        If startGreen < endLB And startGreen <> 0 Then
             startChar = startGreen
             cell.Characters(startLB, endLB - startLB).Font.Color = RGB(0, 153, 0)
        ElseIf startAmber < endLB And startAmber <> 0 Then
            startChar = startAmber
            cell.Characters(startLB, endLB - startLB).Font.Color = RGB(226, 107, 10)
            cell.Characters(startLB, endLB - startLB).Font.Underline = xlUnderlineStyleSingle
        ElseIf startRed < endLB And startRed <> 0 Then
             startChar = startRed
             cell.Characters(startLB, endLB - startLB).Font.Color = RGB(255, 0, 0)
             cell.Characters(startLB, endLB - startLB).Font.Underline = xlUnderlineStyleSingle
             Else
             GoTo MoveOn
        End If

        If startChar = 0 Then GoTo MoveOn      




MoveOn:
Next



Next cell
x = x + 1
Next

TurnON
Application.StatusBar = False

MsgBox "finished"
End Sub

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

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