VBA: Automatically highlight cell when user enter data that does not match with another data from a different column

atisyam

New Member
Joined
Sep 19, 2018
Messages
37
Hello

I am not a programmer so I apologize if my title seems confusing. This is what I meant: Cell D5 will automatically highlight as the data entered does not match with E4 for the same vehicle number. Cell D3 will not be highlighted as the data entered matched with E2 for the same vehicle number. Hope it is possible. Thanks to anyone who can help.

ABCDEF
1Inspection dateVehicle NumberOld S/NNew S/NReason
210/1/18Sm1234-098Bus Arrival
310/2/18Sm1234098345Maintenance
412/2/18Sm5678-213Bus Arrival
512/3/18Sm5678345639Maintenance

<tbody>
</tbody>
 
Hello again Trebor 76

Just to ask, is this code is only applicable for existing information on a sheet? i.e. to highlight the errors to enable individual to visualize the mistake and amend accordingly.

But if I were to create a new row, say continue from E8 onwards, even when I put the correct sticker location first then input the correct "old S/N" (i.e. E8 = 4183. Sticker location = Door), the cell E8 will be in red with the popups.

Super embarrassing but yeah, must the following code be changed to accommodate for any new rows?

Is this what you're after:

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rngMyCell As Range
    Dim dblOldSN As Double
    Dim strStickerLocation As String
    
    Application.ScreenUpdating = False
    
    If Target.Column = 6 And Target.Row >= 3 And Len(Target.Value) > 0 Then
        dblOldSN = Range("F" & Target.Row)
        strStickerLocation = Range("H" & Target.Row)
        For Each rngMyCell In Range("G3:G" & Range("G" & Rows.Count).End(xlUp).Row)
            'Assumes there is only one S/N match in Col. G.
            If rngMyCell = dblOldSN Then
                If rngMyCell.Offset(0, 1) = strStickerLocation Then
                    Target.Interior.Color = xlNone
                    Exit For
                End If
            Else
                Target.Interior.Color = RGB(255, 0, 0)
                If MsgBox("Serial No. does not match with previous record. Continue?", vbRetryCancel, "Incorrect Serial No.") = vbRetry Then
                    Target.Select
                    Exit For
                Else
                    MsgBox "Red cell will denote as error", vbInformation
                    Exit For
                End If
            End If
        Next rngMyCell
    End If

    Application.ScreenUpdating = True

End Sub

Please use the correct tags i.e. [CODE] Code goes here [/CODE] when posting code.

Robert
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The code will only work on the sheet it's attached to. It works when a cell in column F from Row 3 is changed (you changed it from column E to F :confused:). Once a change in any cell from column F from Row 3 has been changed the code works down column G from G3 to whatever the last row is in column G - no change in the code required.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top