搜索匹配的文本字符串并更改字体颜色

时间:2013-08-01 16:56:39

标签: string vba excel-vba excel-formula excel-2010

我使用Excel已经有6年了,而且我有点生疏了。这是我的情景:

我正在将问题列表导出到Excel。我需要能够区分单元格中的相关链接数(多个值)。例如,我有两列,

Key =票证号码

链接问题=相关关键字

我需要一个语句来扫描Key列并在Linked Issues列中找到匹配项。然后,一旦找到匹配,匹配的文本将采用密钥的字体颜色。

如果这变得复杂,则链接问题列的每个单元格看起来都像这样的iss-3913,iss-3923,iss-1649。所以基本上扫描将是字符串中的匹配。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

对不起,我现在没有时间完成这个,但是这样的事情会帮助与第一列中每个单元格的循环

编辑:现在完成,第二次修改以更新到B5和Z5,使用列引用编辑3固定的goof并更新以使用变量来指定要查看的列。

Sub colortext()
start_row = 5
key_col = 2
linked_col = 26
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
    o = start_row 'start with row one for second column
    Do While Not IsEmpty(Cells(o, linked_col)) 'Do until empty cell
    If Not InStr(1, Cells(o, linked_col), Cells(i, key_col)) = 0 Then  'if cell contents found in cell
        With Cells(o, linked_col).Characters(Start:=InStr(1, Cells(o, linked_col), Cells(i, key_col)), Length:=Len(Cells(i, key_col))).Font
            .Color = Cells(i, key_col).Font.Color  'change color of this part of the cell
        End With
    End If
    o = o + 1 'increment the cell in second column
    Loop
    i = i + 1 'increment the cell in the first column
Loop
End Sub

或者

这样的东西?

Excel VBA: change font color for specific char in a cell range

答案 1 :(得分:1)

这是一篇很老的帖子,但我认为我会提供有关我所遇到的条件格式问题的工作。

Sub colorkey()
start_row = 5
key_col = 2
flag_col = 4

i = start_row 'start on row one

  Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell

  Tval = Cells(i, flag_col).Value
    Select Case Tval
    Case "Requirement"
        'cval = green
        cVal = 10
    Case "New Feature"
        'cval = orange
        cVal = 46
    Case "Test"
        'cval = lt blue
        cVal = 28
    Case "Epic"
        'cval = maroon
        cVal = 30
    Case "Story"
        'cval = dk blue
        cVal = 49
    Case "Theme"
        'cval = grey
        cVal = 48
    Case "Bug"
        'cval = red
        cVal = 3
    Case "NOT MAPPED"
        'cval = Maroon
        cVal = 1
    End Select


Cells(i, key_col).Font.ColorIndex = cVal

    i = i + 1 'increment the cell in the first column
    Loop

End Sub
Sub colorlinked()
start_row = 5
key_col = 2
linked_col = 26
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
    o = start_row 'start with row one for second column
    Do While Not IsEmpty(Cells(o, linked_col)) 'Do until empty cell
    If Not InStr(1, Cells(o, linked_col), Cells(i, key_col)) = 0 Then  'if cell contents found in cell
        With Cells(o, linked_col).Characters(Start:=InStr(1, Cells(o, linked_col), Cells(i, key_col)), Length:=Len(Cells(i, key_col))).Font
            .Color = Cells(i, key_col).Font.Color  'change color of this part of the cell
        End With
    End If
    o = o + 1 'increment the cell in second column
    Loop
    i = i + 1 'increment the cell in the first column
Loop
MsgBox "Finished Scanning"
End Sub