简体   繁体   中英

If Value Doesn't Match Table, Highlight - VBA

I'm looking to loop through column A, and highlight the cell if the value in column C doesn't correspond to the value in a table. For example:

A:C栏

表

In the first image, you can see in row 3, the name 'Smith, John' has user 'RKE' which isn't the correct user, when referencing the table. I'm able to set up the following, but I'm stuck there.

Application.ScreenUpdating = False
Dim i As Integer

Sheets("Added(Physical)").Select
Range("A1").Select
lrow = Range(Selection, Selection.End(xlDown)).Count

For i = 2 To lrow

    If Cells(1, i).Value = worksheetapplication.if().Value Then

        i.Interior.Color = 5287936
    End If

Next i

Logically, I would run a vlookup on column A and C, and if there is an error on the vlookup, then the cell color would need to be highlighted.

Anyone have any thoughts on this?

You cant use vlookup because it will find "Smith, John" no matter what column C is. I will use multiple criteria countifs, if it returns 0 (not found), then color it.

Application.CountIfs(Range("A4:A9"), Cells(1, 1).Value, Range("C4:C9"), Cells(1, 3).Value)

The range will be your lookup table (fixed range). The cells.value will be what you're looking for. You can put that inside your loop ie cells(1+n,1)

I ended up using the snip below:

For i = 2 To lrow

Cells(i, 1).Activate
x = Application.VLookup(ActiveCell.Offset(0, 0).Value, Worksheets("MiscDefinitions").Range("G1:H11"), 2, False)
    If x <> Cells(i, 4).Value Then
        Cells(i, 4).Interior.Color = RGB(255, 0, 0)
    Else
    End If
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM