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

Excel 根据该行第二个单元格的值为整行着色?,excel,excel-2007,Excel,Excel 2007,我在Excel中有一个列表,我需要根据该行单元格2中的值设置行的格式。这就是数据的样子 No. | Name | Other data | Other data 2 | Date | Date 2 | 例如,if Name=John Tery=>将行颜色设置为红色,if Name=Mary Jane=>将行颜色设置为粉色,等等 我尝试使用条件格式,但我不知道如何使它工作。我对Excel中的此类任务几乎没有经验 有人能帮忙吗 注:所有的名字都是两个单词的名字编辑重读这个问题,我发现整行的颜色不仅

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

No. | Name | Other data | Other data 2 | Date | Date 2 |
例如,
if Name=John Tery=>将行颜色设置为红色
if Name=Mary Jane=>将行颜色设置为粉色
,等等

我尝试使用条件格式,但我不知道如何使它工作。我对Excel中的此类任务几乎没有经验

有人能帮忙吗


注:所有的名字都是两个单词的名字

编辑重读这个问题,我发现整行的颜色不仅仅是名字。我还决定,如果一个已识别的名称被一个未识别的名称替换,则应从行中删除颜色。已替换原始代码以解决这些问题

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

首先,你需要一些方法来确定“约翰·特里”是红色的,“玛丽·简”是粉红色的。我决定最简单的方法是制作一份工作表
NameColour
,其中列出了按要求上色的名称。所以例程知道“John Tery”是红色的,因为它在这个列表中是红色的。我在你的名单上又加了几个名字。例程不关心一个名称中有多少个单词

下面的代码必须放入本工作簿中。每当单元格发生更改时,就会触发此例程。变量
MonitorColNum
MonitorSheetName
告诉例程要监视哪个表和列。忽略任何其他单元格更改。如果找到匹配项,则从NameColor复制名称的标准格式(如果不需要,则从代码中删除此语句),并根据需要为单元格着色。如果未找到匹配项,则会将名称添加到NameColor,以便以后指定其颜色

希望这有帮助

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
选项显式
私有子工作簿(ByVal Sh作为对象,ByVal作为范围)
Dim CellCrnt作为变体
暗领一样长
Dim被发现为布尔值
暗淡的监视器长度为
Dim MonitorSheetName作为字符串
昏暗的长廊
MonitorSheetName=“Sheet2”
MonitorColNum=2
因此,对受监控单元的更改不会触发此例程
Application.EnableEvents=False
如果Sh.Name=MonitorSheetName,则
'使用标题行中的最后一个值确定颜色范围
ColLast=Sh.Cells(1,Columns.Count).End(xlToLeft).Column
对于更改中的每个CellCrnt
如果CellCrnt.Column=MonitorColNum,则
带工作表(“名称颜色”)
RowNCCrnt=1
发现=错误
执行While.Cells(RowNCCrnt,1).Value“”
如果LCase(.Cells(rownccrn,1.Value)=LCase(cellcrn.Value),则
"确保标准案例",
CellCrnt.Value=.Cells(RowNCCrnt,1).Value
'将所需颜色设置为名称
'CellCrnt.Interior.Color=.Cells(RowncRnt,1).Interior.Color
'将所需颜色设置为行
Sh.范围(Sh.单元格(CellCrnt.Row,1)_
Sh.Cells(CellCrnt.Row,ColLast)).Interior.Color=_
.单元格(RowNCCrnt,1).内部颜色
找到=真
退出Do
如果结束
RowNCCrnt=RowNCCrnt+1
环
如果没有找到的话
'未找到名称。添加到列表中,以便稍后指定其颜色
.Cells(RowNCCrnt,1).Value=CellCrnt.Value
“清除任何现有颜色
Sh.范围(Sh.单元格(CellCrnt.Row,1)_
Sh.Cells(CellCrnt.Row,ColLast)).Interior.ColorIndex=xlNone
如果结束
以
如果结束
下一个
如果结束
Application.EnableEvents=True
端接头

编辑重读这个问题,我发现整行都要上色,而不仅仅是名字。我还决定,如果一个已识别的名称被一个未识别的名称替换,则应从行中删除颜色。已替换原始代码以解决这些问题

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

首先,你需要一些方法来确定“约翰·特里”是红色的,“玛丽·简”是粉红色的。我决定最简单的方法是制作一份工作表
NameColour
,其中列出了按要求上色的名称。所以例程知道“John Tery”是红色的,因为它在这个列表中是红色的。我在你的名单上又加了几个名字。例程不关心一个名称中有多少个单词

下面的代码必须放入本工作簿中。每当单元格发生更改时,就会触发此例程。变量
MonitorColNum
MonitorSheetName
告诉例程要监视哪个表和列。忽略任何其他单元格更改。如果找到匹配项,则从NameColor复制名称的标准格式(如果不需要,则从代码中删除此语句),并根据需要为单元格着色。如果未找到匹配项,则会将名称添加到NameColor,以便以后指定其颜色

希望这有帮助

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
选项显式
私有子工作簿(ByVal Sh作为对象,ByVal作为范围)
Dim CellCrnt作为变体
暗领一样长
Dim被发现为布尔值
暗淡的监视器长度为
Dim MonitorSheetName作为字符串
昏暗的长廊
MonitorSheetName=“Sheet2”
MonitorColNum=2
因此,对受监控单元的更改不会触发此例程
Application.EnableEvents=False
如果Sh.Name=MonitorSheetName,则
'使用标题行中的最后一个值确定颜色范围
ColLast=Sh.Cells(1,Columns.Count).End(xlToLeft).Column
对于更改中的每个CellCrnt
如果CellCrnt.Column=MonitorColNum,则
与沃什