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
 
Hi, Jim May.

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

This will be executed no matter because when there is an OR operator, if either one of the statement is true, then the if condition will run.
Just as a reminder, the code will execute when condition is true.
Rich (BB code):
If condition Then
    code
End If

So, in your case, your condition was
Target.Column <> 4 Or Target.Column <> 5

Therefore, even if your target column is 4,
Target.Column <> 4 will return False while
Target.Column <> 5 will return True.
resulting into
If False Or True Then Exit Sub
same as
If True Then Exit Sub
allowing your code to exit the sub no matter what.

However, when you use And operator,
If False And True Then Exit Sub
same as
If False Then Exit Sub
as And operator requires both conditions to be satisfied in order to return True as a whole.

Therefore, you should use And instead of Or operator in this case.


Tips:
When you are using negativity in both conditions, Or and And operator will change with one another from when you're using positivity.
For example,
If Target.Column = 4 Or Target.Column = 5 Then
is opposite from
If Target.Column <> 4 And Target.Column <> 5 Then

I hope this helps.
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Thanks again to ALL you guys (especially kpark92) for taking the time to offer such great explanations. This grass-roots logic always takes me "near the edge" of the abyss. LOL
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

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