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

时间:2012-04-07 08:47:05

标签: excel excel-2007

我在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中对此类任务的经验很少。

有人可以帮忙吗?

PS。所有名称都是双字名称

2 个答案:

答案 0 :(得分:3)

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

=$B2="John Tery"
  • 您需要从顶行向下选择受影响的行(因此当前活动单元格位于第2行,而不是最后一行)
  • 对列$B的绝对引用意味着对于不同列中的所有单元格,将对B列进行测试
  • 对行2的相对引用意味着对于不同行中的单元格,将测试其自己的行(例如,对于单元格A42,该公式将测试$ B42的值)
  • 等于运算符=将返回TRUE或FALSE(如果任何参数是错误,则返回错误),它与IF条件内部具有相同的用途......

答案 1 :(得分:2)

编辑重新阅读问题,我看到整个行不仅仅是名称。我还决定,如果识别的名称被无法识别的名称替换,则应该从行中删除颜色。原始代码已被替换以解决这些问题。

我决定不关心我的问题的答案,因为下面的解决方案对于我能识别的任何场景来说都是最简单的。

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

Worksheet NameColour showing coloured names

以下代码必须放在ThisWorkbook中。每当更改单元格时都会触发此例程。变量MonitorColNumMonitorSheetName告诉例程要监视哪个工作表和列。忽略任何其他单元格更改。如果找到匹配项,它将从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