PC Review


Reply
Thread Tools Rate Thread

2007 Even/Odd/MOD

 
 
Otto Moehrbach
Guest
Posts: n/a
 
      17th Nov 2009
Excel 2007, Win 7 64-bit
In the past, I've always used 'MOD of X/2 =0' to determine if X is odd or
even. Has Microsoft come up with a shorter VBA function/command to do this
in 2007? Thanks for your time. Otto

 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      17th Nov 2009
Hi,

Nothing new I'm aware of but you can do this

q = WorksheetFunction.IsEven(MyNumber)

Mike

"Otto Moehrbach" wrote:

> Excel 2007, Win 7 64-bit
> In the past, I've always used 'MOD of X/2 =0' to determine if X is odd or
> even. Has Microsoft come up with a shorter VBA function/command to do this
> in 2007? Thanks for your time. Otto
>
> .
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Nov 2009
This is not new to XL2007; but, when using VB code, you can And your number
with 1 to see if it is odd or even. I believe doing it this way is even
quicker than using the Mod method (although that would only be significant
in a large loop). Consider this code...

If YourNumber And 1 Then
MsgBox "Your number is Odd"
Else
MsgBox "Your number is Even
End If

The logical expression "YourNumber And 1" returns either 1 if the number is
odd or 0 if the number is even, so you can test explicitly for those values
if you need or want to, but in an If..Then statement, 1 evaluates to True
and 0 evaluates to False, so normally you do not need to make that explicit
test (notice I left off the =1 from my test in my example code).

--
Rick (MVP - Excel)


"Otto Moehrbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Excel 2007, Win 7 64-bit
> In the past, I've always used 'MOD of X/2 =0' to determine if X is odd or
> even. Has Microsoft come up with a shorter VBA function/command to do
> this in 2007? Thanks for your time. Otto


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Nov 2009
> ...in an If..Then statement, 1 evaluates to True and 0 evaluates to False

Just to clarify the above statement, the "general rule" is that in an
If..Then statement, 0 evaluates to False and **any** non-zero number equates
to True. The only two values "YourNumber And 1" can produce are 0 and 1,
hence my original statement.

--
Rick (MVP - Excel)


"Rick Rothstein" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> This is not new to XL2007; but, when using VB code, you can And your
> number with 1 to see if it is odd or even. I believe doing it this way is
> even quicker than using the Mod method (although that would only be
> significant in a large loop). Consider this code...
>
> If YourNumber And 1 Then
> MsgBox "Your number is Odd"
> Else
> MsgBox "Your number is Even
> End If
>
> The logical expression "YourNumber And 1" returns either 1 if the number
> is odd or 0 if the number is even, so you can test explicitly for those
> values if you need or want to, but in an If..Then statement, 1 evaluates
> to True and 0 evaluates to False, so normally you do not need to make that
> explicit test (notice I left off the =1 from my test in my example code).
>
> --
> Rick (MVP - Excel)
>
>
> "Otto Moehrbach" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Excel 2007, Win 7 64-bit
>> In the past, I've always used 'MOD of X/2 =0' to determine if X is odd or
>> even. Has Microsoft come up with a shorter VBA function/command to do
>> this in 2007? Thanks for your time. Otto

>


 
Reply With Quote
 
Otto Moehrbach
Guest
Posts: n/a
 
      17th Nov 2009
Rick, Mike
Thanks for that, both of you. Something new. Otto

"Otto Moehrbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Excel 2007, Win 7 64-bit
> In the past, I've always used 'MOD of X/2 =0' to determine if X is odd or
> even. Has Microsoft come up with a shorter VBA function/command to do
> this in 2007? Thanks for your time. Otto


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WINDOWS VISTA, WINDOWS XP ( X64 ) PRO CORP SP2, OFFICE 2007, PROJECT 2007, PUBLISHER 2007, HACKING LESSONS 1-17, other 2006/December/20 new programs leo Windows XP Basics 5 27th Dec 2006 03:18 PM
WINDOWS VISTA, WINDOWS XP ( X64 ) PRO CORP SP2, OFFICE 2007, PROJECT 2007, PUBLISHER 2007, HACKING LESSONS 1-17, other 2006/December/20 new programs kashumoto_tokugawa Windows XP Embedded 0 20th Dec 2006 04:16 AM
WINDOWS VISTA, WINDOWS XP ( X64 ) PRO CORP SP2, OFFICE 2007, PROJECT 2007, PUBLISHER 2007, HACKING LESSONS 1-17, other 2006/December/20 new programs leo Microsoft Dot NET 0 20th Dec 2006 04:08 AM
WINDOWS VISTA, WINDOWS XP ( X64 ) PRO CORP SP2, OFFICE 2007, PROJECT 2007, PUBLISHER 2007, HACKING LESSONS 1-17, other 2006/December/20 new programs leo Microsoft VB .NET 0 20th Dec 2006 04:02 AM
OFFICE 2007 ENTERPRISE UG VL (OFFICE-SOLUTION), PROJECT 2007, PUBLISHER 2007, BRITANNICA 2007, other 2006-11-26 new programs CDs 5200 - 5227, and new games CDs 1945 - 1959 kashumoto_tokugawa Windows XP General 0 26th Nov 2006 12:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:07 PM.