簡體   English   中英

根據行中的另一個單元格值更改實際字體顏色

[英]Change actual font color based on another cell value in the Row

我目前有一個工作表,它調查B列並將Z列中的字符串與String匹配,然后將匹配的String的顏色更改為B列中的font.color。問題是B列通過條件格式着色,因此代碼無法識別。 當條件為真時,我需要能夠在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

塊引用

萬一您只是希望“您嘗試過的解決方案”起作用,這是使條件格式起作用的方式:

  1. 選擇要對其應用條件格式的單元格(在B列中)
  2. 單擊“條件格式”按鈕。 清除所有不再需要的規則,然后基於“等式為真”創建“新規則”
  3. 輸入以下公式: =ISNUMBER(SEARCH(B1, "EPIC"))
  4. 選擇其中包含文本“ EPIC”的單元格所需的格式(請注意-以“ SEARCH”的順序,我們在B1中查找包含在短語“ EPIC”中的文本,因此“ E”將匹配,就像“ IC”一樣。如果只希望匹配“ That was EPIC”單元格,則需要顛倒參數的順序
  5. 為您要匹配的其他單詞和所需顏色添加更多規則

創建單個規則后,對話框將如下所示:

在此處輸入圖片說明

這是完成第二條規則后的“條件格式”對話框的樣子(在我的示例中,我將這些規則應用於8個單元格):

在此處輸入圖片說明

此時,電子表格如下所示:

在此處輸入圖片說明

這似乎是您所要的...如果不是,請在評論中進行說明!

擺脫條件格式很容易:

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變量中,然后根據需要應用於單元格。

這是制勝法寶:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM