如果单元格包含值,则为整行排序

时间:2016-07-28 17:02:35

标签: excel vba excel-vba

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

  <div class="row" style=" margin-top:0px;height:100px; ">
    <div class="col col-25" style="background-color:#c8c8c8;text-align:center;padding:0px;">
      <div>
        <img src="img/task.png" style="display:block; margin:auto;margin-top:10px;">
        <div style="margin-top:20px; margin-left:5px;"><label style="font-size:11px;">My Tasks</label></div>
      </div>
    </div>
    <div class="col col-50" style="background-color:#413d3a;text-align:center;padding:0px; ">
      <img src="img/document.png" style="display:block; margin:auto;margin-top:10px;">
      <div style="margin-top:20px; margin-left:5px;"><label style="font-size:11px; color:#fff;">My Documents</label></div>
    </div>
    <div class="col col-25" style="background-color:#c8c8c8;text-align:center;padding:0px; ">
      <img src="img/Newsfeed.png" style="display:block; margin:auto;margin-top:10px;">
      <div style="margin-top:20px; margin-left:5px;"><label style="font-size:11px; ">Newsfeed</label></div>
    </div>
  </div>

我在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

上收到了一个对象错误

1 个答案:

答案 0 :(得分:2)

我假设如果单元格包含&#34; C&#34;并且不包含&#34; P&#34;然后给它着色。

实施例

  • &#34; ABCDEF&#34; :给它着色
  • &#34; ABCP&#34; :不要着色它
  • &#34; ABC&#34; :给它着色
  • &#34; DEFGH&#34; :不要着色它
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