how to use Intersect

LFKim2018

Active Member
Joined
Mar 24, 2018
Messages
267
I have a change event sub below and I want to trap changes made on columns A to B only.
How do I use Intersect on this sub?
many thanks

Code:
Dim rngLastChange As Range


Private Sub Worksheet_Change(ByVal Target As Range)
    Set rngLastChange = Target
    If [E2] > [F2] Then       
        MsgBox "E2 is greater than F2", vbCritical
        Application.Undo
        Exit Sub
    End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
So if you change any value in column A or B
You want the script to see if cell in column E is greater then cell in column F On same row

Is that what you want?
And this change will be done manually?
Not as the result of a formula change.
 
Upvote 0
Thank you for taking time to answer my query.
So if you change any value in column A or B
You want the script to see if cell in column E is greater then cell in column F On same row
yes that's it. and I think I get it - tested it to be working - pls see below if it is the correct coding structure.
many many thanks

Dim rngLastChange As Range


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Set rngLastChange = Target
    [B]    If Intersect(Target, Me.Range("A:B")) Is Nothing Then Exit Sub[/B]
    If [E2] > [F2] Then   
        MsgBox "E2 is greater than F2", vbCritical
        Application.Undo
        Exit Sub
    End If
End Sub
 
Last edited:
Upvote 0
Yes that will work.

So you only want to compare E2 and F2

Not:
E3 and F3
or E4 and F4
 
Upvote 0
What is variable rngLastChange used for? If not used, no point leaving in. As alt suggestion try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column > 3 Then Exit Sub
    
    If Cells(2, 5).Value > Cells(2, 6).Value Then
        MsgBox "WARNING E2 is greater than F2", vbCritical, "Invalid value in E2 or F2"
        Application.Undo
    End If
    
End Sub
 
Last edited:
Upvote 0
That would create an infinite loop jack as the undo will keep triggering the procedure
 
Upvote 0
Ah hence the Exit Sub after (or Application.EnableEvents = False/True wrap), thanks Steve :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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