Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如果单元格包含值,则为整行着色_Vba_Excel - Fatal编程技术网

Vba 如果单元格包含值,则为整行着色

Vba 如果单元格包含值,则为整行着色,vba,excel,Vba,Excel,我试图给特定列中包含“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",

我试图给特定列中包含“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”:不要给它上色


您删除了一个句点
.Value=IIf(.Interior.ColorIndex=15,“C”,“P”)
,因此Interior不是指范围。Interior为什么不使用条件格式?虽然这可以解释您的错误,但它无法修复您的代码,这与您描述的代码不符。条件格式确实是最好的。如果您出于某种原因不想使用它,那么需要进行大量的代码重写才能完成您所描述的工作。这会更好。非常感谢。我经常混淆VBA中的函数。我想我只是需要更多的经验。我很感激!我建议看Youtube上的所有视频。
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