根据行中的另一个单元格值更改实际字体颜色

时间:2013-08-06 20:42:54

标签: excel vba excel-vba excel-2010 conditional-formatting

我目前有一个工作表,它查看B列并将字符串与Z列中的字符串匹配,然后将匹配字符串的颜色更改为B列中的font.color。问题是B列是通过条件格式设置着色的所以代码无法识别。当condtion为true时,我需要能够在B列中进行实际的字体颜色更改。此外,代码需要递增,直到达到工作表的最后一行。

这是我设置的当前条件格式

  

块引用

=ISNUMBER(SEARCH("Story",Template!D5))=TRUE 'format dark blue
=ISNUMBER(SEARCH("Requirement",Template!D5))=TRUE 'format green
=ISNUMBER(SEARCH("EPIC",Template!D5))=TRUE 'format red
=ISNUMBER(SEARCH("Test",Template!D5))=TRUE 'format teal
=ISNUMBER(SEARCH("New Feature",Template!D5))=TRUE 'format orange
=ISNUMBER(SEARCH("Theme",Template!D5))=TRUE 'format gray
  

块引用

Sub Main()
  Call NoLinks
  Call SetCellWarning
  Call colortext
End Sub

Sub NoLinks()
ActiveSheet.Hyperlinks.Delete
End Sub

Sub SetCellWarning()
    Dim iLastRow As Long
    Dim cel As Range, rSetColumn As Range

    iLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row

    Set rSetColumn = Range(Cells(5, 26), Cells(iLastRow, 26)) ' Column "Z"...

    For Each cel In rSetColumn
        If cel.Value = "" Then
            With cel
                cel.Value = "NOT MAPPED"
            End With
        End If
    Next cel

End Sub

***'The colortext runs but does not update unless the font color is manually updated***    
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
  

块引用

3 个答案:

答案 0 :(得分:1)

万一你只是想要"你之前尝试的解决方案"要工作,这是你如何使条件格式工作:

  1. 选择要应用条件格式的单元格(在B列中)
  2. 点击"条件格式化"按钮。清除您不再需要的任何规则,然后创建一个"新规则"基于"方程为真"
  3. 输入以下等式:=ISNUMBER(SEARCH(B1, "EPIC"))
  4. 使用文本" EPIC"选择所需格式的单元格。在它们中(注意 - 按照" SEARCH"的顺序,我们寻找B1中的文字包含在短语" EPIC"中,所以" E"将如果你只想要和#34;那是EPIC"匹配的单元格,你需要改变参数的顺序
  5. 为您要匹配的其他字词添加更多规则,以及您需要的颜色
  6. 这就是刚创建单个规则时对话框的样子:

    enter image description here

    这就是"条件格式化"在完成第二条规则后,对话框就会显示(在我的示例中,我将这些规则应用于8个单元格):

    enter image description here

    此时,电子表格如下所示:

    enter image description here

    这似乎是你要求的......如果不是,那么请在评论中澄清!

答案 1 :(得分:0)

轻松删除条件格式:

If (Cells(i, key_col).FormatConditions.Count > 0) Then
    Cells(i, key_col).FormatConditions.Delete 
End If
.Color = Cells(i, key_col).Font.Color  'change color of this part of the cell

您甚至可以将其存储在FormatCondition变量中,如果愿意,可以稍后应用于单元格。

答案 2 :(得分:0)

以下是获胜的解决方案:

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 = red
    cVal = 3
Case "Story"
    'cval = dk blue
    cVal = 49
Case "Theme"
    'cval = grey
    cVal = 48
Case "NOT MAPPED"
    'cval = Maroon
    cVal = 53
End Select
Cells(i, key_col).Font.ColorIndex = cVal

i = i + 1 'increment the row
Loop

End Sub