Friday time-waster

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,851
Office Version
  1. 365
Platform
  1. Windows
Here's a tiny time-waster for a Friday which may push your understanding of VBA...


Paste the following code into the VBA Immediate window - or make it into a procedure if you're so inclined - and try to guess the outcome in advance:-<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Code:
[FONT=Courier New][COLOR=black][COLOR=black][FONT=Courier New]if 1 = 2 or 3 then msgbox "1 equals 2 or 3"[/FONT][/COLOR][/COLOR][/FONT]
<o:p></o:p>
Was it what you were expecting?
<o:p></o:p>
<o:p></o:p>
Now try these:-<o:p></o:p>
<o:p>
Code:
 [COLOR=black][FONT=Courier New]if 7 = 99 or 1001 then msgbox "7 equals 99 or 1001"[/FONT][/COLOR]
</o:p>

Code:
[COLOR=black][FONT=Courier New]if (6 and 11) = 2 then msgbox "6 and 11 equals 2"[/FONT][/COLOR]

Hands up anyone who got them all right? :)
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Your OR's and AND's are all wrong ;-)

Code:
if 1 = 2 or 1 = 3 then msgbox "1 equals 2 or 3"
would be correct.
 
Upvote 0
Do you have a web cam? How do you know my hand is up? :)
 
Upvote 0
Ruddles

Isn't that something to do with bitwise operations/logic?
 
Upvote 0
Solution
Isn't that something to do with bitwise operations/logic?
Yes indeed, but it also shows what happens when you're thinking If x = 99 Or x = 1001 but you actually code If x = 99 Or 1001. We've had a couple like that on the board in the last day or so.
 
Last edited:
Upvote 0
And the difference between:
Code:
1 = 2 or 3
and:
Code:
1 = (2 or 3)
 
Upvote 0
I have not been staring at this the entire time (drooool...).

Okay, maybe I have. Ouch, my brain! I get that 2 OR 3 returns 3 (ie 10 OR 11 returns 11) but why are the parenthesis needed and what is happening in the first one to get 3 returned?
 
Upvote 0
The first one is effectively False Or 3 (where False = 0) so the result is 3.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,470
Members
452,915
Latest member
hannnahheileen

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