Why is code Exiting?

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
I have a change event set up in sheet1

My enableevents is TRUE;

I;ve been sitting here for 5 minutes changing data in Columns D and E, yet
(while in Step mode) the Exit Sub is executed. Ideas why?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 4 Or Target.Column <> 5 Then Exit Sub
...more code follows
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try

Rich (BB code):
If Target.Column <> 4 And Target.Column <> 5 Then Exit Sub
 
Upvote 0
Target.Column <> 4 Or Target.Column <> 5

4 is not equal 5 and visa versa

TRY

(Target.Column <> 4) AND (Target.Column <> 5)

(Target.Column < 4) Or (Target.Column > 5)
 
Upvote 0
Better yet:
Code:
If Not Intersect(Target, Range("D:E")) Is Nothing then
' run your code
End If
 
Upvote 0
Sorry, but

Thanks to all;

But to CharlesChuckieCharles...

Target.Column <> 4 Or Target.Column <> 5

seems (right now) logical to me; seems like I've used this logic before, havn't I?

and I don't quite get the "4 is not equal 5 and visa versa" follow thru..

I'm pretty THICK, can you put this in "imbecil" terms?
 
Upvote 0
If you are in column 4

Target.Column <> 5

is true and you exit.

If you are in column 5

Target.Column <> 4

is true and you exit.

If you want to check if you are in either column you need And not Or.
 
Upvote 0
if the target column is not 4, and also, the target column is not 5 then it is neither 4 nor 5 , and so exit sub.

you are using negative logic.

positive logic:
if target IS 4 or target IS 5 then it is ok to continue
 
Upvote 0
Put another way, the column cannot be both 4 and 5 so one of the Or statements must always be true.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

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