突出显示行,但不是整行

时间:2017-01-10 13:17:17

标签: excel-vba vba excel

我有一个工作簿,其中包含我的分析师的每日登录信息。我根据各种参数设置了某些条件突出显示。我想要添加的是将搜索列(H)的代码,如果值为0(意味着分析师当天没有登录),则删除该行的条件突出显示,并更改该行变红了。但是,由于该行仅从col A延伸到col R,因此我不希望突出显示S和更高版本。下面是我到目前为止的代码,它一直工作到最后,因为它突出了整行。我知道问题是EntireRow,但我不知道如何让它成为A#:R#,其中#是col H中对应0的行。

'Account for any OOO
    Dim srng As Range
    For Each srng In Range("H2:H8")
        If srng.Value = 0 Then
            srng.EntireRow.FormatConditions.Delete
            srng.EntireRow.Interior.Color = 5263615
        End If
    Next srng

EDIT ::::::::

我最终想要做的是在col H为0的情况下,然后提示我考虑分析师是否已经安排好了。如果是,请以灰色突出显示该行;如果没有突出显示红色下面的代码不太正确,但我试过..

'Account for any OOO
    Dim schdOff As Range
    For Each schdOff In Range("H2:H8")
        If schdOff.Value = 0 Then
            Dim int1 As Integer
            Dim sPrompt As String
            sPrompt = "Was the analyst scheduled to be off yesterday?"
            int1 = MsgBox(sPrompt, vbYesNo)
                If int1 = vbYes Then
                    Cells(schrng.Row, 1).Resize(, 18).FormatConditions.Delete
                    Cells(schrng.Row, 1).Resize(, 18).Interior.Color = 11711154
                Else
                    Cells(srng.Row, 1).Resize(, 18).FormatConditions.Delete
                    Cells(srng.Row, 1).Resize(, 18).Interior.Color = 5263615
                End If
        End If
    Next schdOff

1 个答案:

答案 0 :(得分:2)

试试这个(虽然为什么不使用条件格式?)

'Account for any OOO
    Dim srng As Range
    For Each srng In Range("H2:H8")
        If srng.Value = 0 Then
            cells(srng.Row,1).resize(,18).FormatConditions.Delete
            cells(srng.Row,1).resize(,18).Interior.Color = 5263615
        End If
    Next srng