if VBA

jtodd

Board Regular
Joined
Aug 4, 2014
Messages
194
Hi
How do check if H2 = B2 and have a message if not , I have this so far.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "H2" Then
If Target.Value <> "B2" Then
MsgBox "Wrong Sku!"
End If
End If
End Sub

Would like it to work on all cells in H and B
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Or Target.Column = 8 Then
    If Cells(Target.Row, 2).Value = Cells(Target.Row, 8).Value Then
        MsgBox Cells(Target.Row, 2).Address & " MATCHES " & Cells(Target.Row, 8).Address
    Else
        MsgBox Cells(Target.Row, 2).Address & " DOES NOT MATCH " & Cells(Target.Row, 8).Address
    End If
End If
End Sub
 
Upvote 0
Or this, which also clears the value of the Target cell if SKU is not correct

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = 1 Or Target.Column <> 8 Then Exit Sub

    If Target.Value <> Target.Offset(, -6).Value Then
        MsgBox Target.Value & " is incorrect Sku!"
            With Target
                 Application.EnableEvents = False
                .ClearContents
                .Select
                Application.EnableEvents = True
            End With
    End If

End Sub
 
Upvote 0
First thanks for quick reply.

The code work fine until i use a vlookup for H2 ,
H2 looks up G2 , when I enter data in G2 - H2 changes but the code does not run?
 
Upvote 0
You are using an Event macro which is triggerred when the cell is EDITED
Value returned by formula does not trigger an event macro when the value returned changes
 
Last edited:
Upvote 0
Thanks that explains it .
Is there way of making this work when trigered by a formula?
 
Upvote 0
Not directly

- what is triggering the value returned by VLOOKUP to change?
- are you manually editing a value elsewhere?
 
Upvote 0
I do not know if the macro will be triggered or not :confused:
- give it a go and let me know

The macro is triggered when a value in column G is edited
- the value in column B is then compared with the value in column H

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = 1 Or Target.Column <> 7 Then Exit Sub

    If Target.Offset(, 1).Value <> Target.Offset(, -5).Value Then
        MsgBox Target.Value & " is incorrect Sku!"
        Target.Select
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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