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>
 
I can't post tables through reply so I will start a new thread.

No, please stick to one thread (I've merged your follow-up into this thread)

There shouldn't be anything different about replying vs creating a new thread.
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Yep. Thanks RoryA. Seen the merged with my originally posted thread titled: "VBA: Automatically highlight cell when data entered does not match with data in other columns. If match, cell turns to 'no fill.' "

I will still be playing around with the given code by Trebor76 and see if I can get anywhere. :oops:

No, please stick to one thread (I've merged your follow-up into this thread)

There shouldn't be anything different about replying vs creating a new thread.
 
Upvote 0
Try this event macro on the sheet in question (note the assumption about the S/N only appearing once in Col. F):

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

    Dim lngMyRow As Long
    Dim rngMyCell As Range
    Dim dblOldSN As Double
    Dim strStickerLocation As String
    
    Application.ScreenUpdating = False
    
    If Target.Column = 5 And Target.Row >= 2 And Len(Target.Value) > 0 Then
        dblOldSN = Range("E" & Target.Row)
        strStickerLocation = Range("G" & Target.Row)
        For Each rngMyCell In Range("F2:F" & Range("F" & Rows.Count).End(xlUp).Row)
            'Assumes there is only one S/N match in Col. F.
            If rngMyCell = dblOldSN Then
                If rngMyCell.Offset(0, 1) = strStickerLocation Then
                    Target.Interior.Color = xlNone
                    Exit For
                Else
                    Target.Interior.Color = RGB(255, 0, 0)
                    Exit For
                End If
            End If
        Next rngMyCell
    End If
    
    Application.ScreenUpdating = True

End Sub

Robert
 
Upvote 0
Thank you for the reminder. I'll take note of it for any future thread.

And the code worked! Pretty amazing. I will be putting some msg-boxes to complement the overall coding.

Thanks so much Robert! Your help was super helpful :)
 
Upvote 0
Hi there. I feel like I said this too soon. Now I am having issues with my msg-boxes. Sorry.

Once the cell turns red, a pop up box will appear with a retry and cancel option. If the user press retry, the cell will return back and allow the user to re-enter the data (E.g. E4 will turn red when entered 3847, popup, user choose to retry, return back to E4 to re-enter). If still incorrect the same pop-up box will reappear until the user press cancel so another pop up information will denote that cell will remain red and macro will just continue as per normal.

Here is my code. Changes to column and row alphabets/numbers are made applicable to actual excel sheet.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)


Dim lngMyRow As Long
Dim rngMyCell As Range
Dim dblOldSN As Double
Dim strStickerLocation As String
Dim Output As Integer



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
Else
Target.Interior.Color = RGB(255, 0, 0)
Output = MsgBox("Serial No. does not match with previous record. Continue?", vbRetryCancel, "Incorrect Serial No.")
If Output = vbRetry Then
ActiveCell.Select
Else
MsgBox "Red cell will denote as error", vbInformation
Exit For
End If
End If
Next rngMyCell
End If

Application.ScreenUpdating = True


End Sub

I just cannot get this right. Will continue looking up for other methods to solve this. Will appreciate the solution to this. Thank yoouu





Thanks for letting us know. I'm glad we got it sorted :)
 
Upvote 0
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
I see. Noted for the coding tag, will take note for future postings. And yep, your code is per what I intended.

Thanks again Mr Robert! :)
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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