条件格式行基于IFERROR VLOOKUP结果

时间:2017-11-22 17:10:06

标签: vba vlookup conditional-formatting

我试图在A列中对我的IFERROR VLOOKUP公式失败的每一行进行格式化。我已经到了可以让IFERROR单元格突出显示的位置,但现在不是整个。请帮忙。这就是我到目前为止所做的:

Range("A2:AH2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=""Not VA Student"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

1 个答案:

答案 0 :(得分:0)

我不清楚您的IFERROR VLOOKUP是什么意思,因为我在您的代码中看不到这一点。

我认为您只想突出显示在规定范围内找到“Not VA Student”的行。您当前的代码为单个单元格执行此操作。

我认为选择的范围可能过大,因为您在指定的列限制内选择了工作表的底部。

尝试以下方法。我对现有代码进行了少量修改,并将其归入user3598756

Private Sub HighlightRows()

        Dim wb As Workbook
        Dim ws As Worksheet
        Dim FoundCell As Range
        Dim rng As Range
        Dim FirstFound As String
        Set wb = ThisWorkbook
        Set ws = wb.Worksheets("Sheet1") ' change as appropriate

        Const fnd As String = "Not VA Student"

        With ws.Range("A2:AH" & ws.Range("AH2").End(xlDown).Row) '<--| reference the range to search into ''@qharr: this seems excessive to me

            Set FoundCell = .Find(what:=fnd, after:=.Cells(.Cells.Count))  '<--| find the first cell

            If Not FoundCell Is Nothing Then 'Test to see if anything was found

                FirstFound = FoundCell.Address ' <--| store the first found cell address
                Set rng = FoundCell '<--| initialize the range collecting found cells. this to prevent first 'Union()' statement from failing due to 'rng' being 'Nothing'

                Do

                    Set rng = Union(rng, FoundCell)  'Add found cell to rng range variable

                    'Find next cell with fnd value
                    Set FoundCell = .FindNext(after:=FoundCell)
                Loop While FoundCell.Address <> FirstFound 'Loop until cycled through all finds

            End If

        End With

      If Not rng Is Nothing Then rng.EntireRow.Interior.Color = 65535

End Sub