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>
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi atisyam,

Welcome to MrExcell!!

Try this event macro (i.e. has to go on the sheet in question not in a standard separate module):

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

    If Target.Column = 4 And Target.Row >= 3 Then
        If Target.Value <> Range("E" & Target.Row - 1) Then
            'Fill cell red if it does not match what's in
            'Col. E for the row above. Change to suit.
            Target.Interior.Color = RGB(255, 0, 0)
        Else
            'Clear any previous colour coding as the S/N's match
            Target.Interior.Color = xlNone
        End If
    End If

End Sub

Hope that helps,

Robert
 
Last edited:
Upvote 0
Hi , I think this method will not work unless you come out of cell.

Hi atisyam,

Welcome to MrExcell!!

Try this event macro (i.e. has to go on the sheet in question not in a standard separate module):

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

    If Target.Column = 4 And Target.Row >= 3 Then
        If Target.Value <> Range("E" & Target.Row - 1) Then
            'Fill cell red if it does not match what's in
            'Col. E for the row above. Change to suit.
            Target.Interior.Color = RGB(255, 0, 0)
        Else
            'Clear any previous colour coding as the S/N's match
            Target.Interior.Color = xlNone
        End If
    End If

End Sub

Hope that helps,

Robert
 
Upvote 0
Yes, the entry has to be made in the cell (i.e. you can't be in edit mode) before it will work but that's how Excel works. It would be the same even if you did it via Conditional Formatting :confused:
 
Last edited:
Upvote 0
Hello Robert/Trebor76

Appreciate the prompt reply. Your code works great, unfortunately, to a certain extent.

Per my example above, once I entered a data in D5 that does not match with E4 with the same vehicle number, the cell auto highlight in red :) . But if I input the right data (data in D5 and E4 match), the cell still remains red. I've tried to replace xlNone with other no fill/white color online such as xlColorIndexAutomatic or xlColorIndexNone, still no change.

Below is the code that I copy/paste from yours. The changes in 'Target.Column = 6' is because my Old S/N is in column F (the 6th column) and ' ("G" & Target.Row - 1)' is because my new S/N is in column G.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Column = 6 And Target.Row >= 3 Then
If Target.Value <> Range("G" & Target.Row - 1) Then
'Fill cell red if it does not match what's in
'Col. G for the row above. Change to suit.
Target.Interior.Color = RGB(255, 0, 0)
Else
'Clear any previous colour coding as the S/N's match
Target.Interior.Color = xlNone
End If
End If


End Sub



Hope to get an update for this. Thank youuu.
 
Upvote 0
Not sure as it worked for me :confused:

Are 100 percent sure the old and new are the same? Try the EXCAT function on two cells that you believe are the same and see what it produces i.e.

=EXACT(F14,G13)

Will return TRUE if the entry in cells F14 and G13 are the same.
 
Upvote 0
Hello Robert

Did the EXACT function and yep, the old and new are the same. It returns TRUE.


Not sure as it worked for me :confused:

Are 100 percent sure the old and new are the same? Try the EXCAT function on two cells that you believe are the same and see what it produces i.e.

=EXACT(F14,G13)

Will return TRUE if the entry in cells F14 and G13 are the same.
 
Upvote 0
Not sure what's happening then :confused:

Try putting a break point at the start of code (by pressing F9), edit a particular cell in Col. F from Row 3 and then step through the code by pressing F8 to try and see what's happening. You know the code only works on a single cell not a range right?

If you still can't solve it perhaps you could host the workbook (devoid of all sensitive data) so we can have a look.
 
Upvote 0
Hmmm, what I understood the code to be was to be able to detect any matching error whenever column E and column D does not match, with respect to the vehicle number. It could occur at any row. In reality, my workbook has hundreds of rows and there are a lot of other columns in place. I wrote the previous posts just as a trial and maybe, it would be best if I were to lay it all out.

I can't post tables through reply so I will start a new thread. I also do not know how to host the workbook. :( Thanks for all of your help :)


Not sure what's happening then :confused:

Try putting a break point at the start of code (by pressing F9), edit a particular cell in Col. F from Row 3 and then step through the code by pressing F8 to try and see what's happening. You know the code only works on a single cell not a range right?

If you still can't solve it perhaps you could host the workbook (devoid of all sensitive data) so we can have a look.
 
Upvote 0
VBA: Automatically highlight cell when data entered does not match with data in other columns. If re-entered and match, cell turns to 'no fill.'

Hello


I am not a programmer so I apologize if my title seems confusing. This is what I meant: All the vehicle no. are the same. Cells E4 and E6 will auto highlight as the S/N is incorrect, with respect to the sticker location. Cell E5 will not auto highlight as the S/N is correct, with respect to the sticker location.

Then for E4, if the user re-entered '3846', the cell would turn to 'no fill'/clear the cell. For E6, it's not applicable for this example as there are no prior records.


A
B
C
D
E
F
G
1
Reg. No.
Date
Vehicle No.
Old S/N
New S/N
Sticker Location
2
01
12/1/18
SG9876
-
3846
Window
3
02
12/1/18
SG9876
-
3847
Door
4
03
13/2/18
SG9876
3847
4180
Window
5
04
13/2/18
SG9876
3847
4181
Door
6
05
14/2/18
SG9876
4180
4182
Side Panel
7
06
14/2/18
SG9876
4181
4183
Door

<tbody>
</tbody>



Hope that someone could help me out. The VBA code that I hoped for is that the formula is applicable for all rows (in reality I have 700+ rows), but the columns are fixed to these 3 columns (E, F & G). Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

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