如果单元格包含值,整行颜色

我正在尝试在特定列中包含“C”的任何单元格的整行。 还有我不想着色的“P”。 这是我的代码。

Sub color() Dim lastRow As Long With Sheets("MP Parameters") lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row With .Range("K5:K" & lastRow) .Value = IIf(Interior.ColorIndex = 15, "C", "P") End With End With End Sub 

我得到一个对象错误.Value = IIf(Interior.ColorIndex = 15, "C", "P")

我假设如果单元格包含“C”,并不包含“P”,然后颜色。

例子

  • “ABCDEF”:为它着色
  • “ABCP”:不要着色
  • “ABC”:彩色
  • “DEFGH”:不要着色

 Sub color() Dim lastRow As Long Dim x As Long With Sheets("MP Parameters") lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row For x = 5 To lastRow If .Cells(x, "K") Like "*C*" And Not .Cells(x, "K") Like "*P*" Then .Rows(x).Interior.ColorIndex = 15 End If Next End With End Sub