根据该行中第二个单元格的值为整行着色?

我在Excel中有一个列表,我需要根据该行的单元格2中的值格式化行。 这就是数据的样子

No. | Name | Other data | Other data 2 | Date | Date 2 | 

例如, if Name=John Tery => color row as Redif Name=Mary Jane => color row as Pink等。

我尝试使用条件格式,但我不知道如何使这项工作。 我在Excel中对这些任务的经验很less。

谁能帮忙?

PS。 所有的名字都是两个字的名字

如果只有几个名称可以处理,则每个条件格式公式将如下所示

 =$B2="John Tery" 
  • 您需要从最上一行中select受影响的行(所以当前活动单元格位于第二行,而不是最后一行)
  • $B列的绝对引用意味着对于不同列中的所有单元格,列B将被testing
  • 对第2行的相对引用意味着对于不同行中的单元格,其自己的行将被testing(例如对于单元格A42,公式将testing$ B42的值)
  • 相等运算符=将返回TRUE或FALSE(或者如果任何参数是错误,则返回一个错误),它与IF条件中的用法相同。

编辑重读这个问题,我看到整行不仅仅是上色的名字。 我还决定,如果一个被认可的名字被一个无法识别的名字所取代,那么这个颜色应该从该行中删除。 原来的代码已被replace,以解决这些问题。

我决定我不关心我的问题的答案,因为下面的解决scheme似乎是我可以识别的任何场景中最简单的。

首先,你需要一些方法来确定“John Tery”是红色的,“Mary Jane”是粉红色的。 我决定最简单的方法是有一个工作表NameColour列出名称根据需要着色。 所以例程知道“John Tery”是红色的,因为它在这个列表中是红色的。 我已经在列表中添加了更多的名字。 例程不关心名称中有多less个单词。

工作表名称显示颜色的名称

下面的代码必须在ThisWorkbook 。 每当一个单元格被改变时,这个例程被触发。 MonitorColNumMonitorSheetNamevariables告诉例程要监视的表单和列。 任何其他单元格更改都被忽略。 如果find匹配项,则从NameColour中复制名称的标准格式(如果不需要,则从代码中删除此语句)并根据需要为单元格着色。 如果找不到匹配项,则将名称添加到NameColour中,以稍后指定其颜色。

希望这可以帮助。

 Option Explicit Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Changed As Range) Dim CellCrnt As Variant Dim ColLast As Long Dim Found As Boolean Dim MonitorColNum As Long Dim MonitorSheetName As String Dim RowNCCrnt As Long MonitorSheetName = "Sheet2" MonitorColNum = 2 ' So changes to monitored cells do not trigger this routine Application.EnableEvents = False If Sh.Name = MonitorSheetName Then ' Use last value in heading row to determine range to colour ColLast = Sh.Cells(1, Columns.Count).End(xlToLeft).Column For Each CellCrnt In Changed If CellCrnt.Column = MonitorColNum Then With Worksheets("NameColour") RowNCCrnt = 1 Found = False Do While .Cells(RowNCCrnt, 1).Value <> "" If LCase(.Cells(RowNCCrnt, 1).Value) = LCase(CellCrnt.Value) Then ' Ensure standard case CellCrnt.Value = .Cells(RowNCCrnt, 1).Value ' Set required colour to name 'CellCrnt.Interior.Color = .Cells(RowNCCrnt, 1).Interior.Color ' Set required colour to row Sh.Range(Sh.Cells(CellCrnt.Row, 1), _ Sh.Cells(CellCrnt.Row, ColLast)).Interior.Color = _ .Cells(RowNCCrnt, 1).Interior.Color Found = True Exit Do End If RowNCCrnt = RowNCCrnt + 1 Loop If Not Found Then ' Name not found. Add to list so its colour can be specified later .Cells(RowNCCrnt, 1).Value = CellCrnt.Value ' Clear any existing colour Sh.Range(Sh.Cells(CellCrnt.Row, 1), _ Sh.Cells(CellCrnt.Row, ColLast)).Interior.ColorIndex = xlNone End If End With End If Next End If Application.EnableEvents = True End Sub