Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2008

Tip: Looking for answers? Try searching our database.

even

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ranswert - 18 Mar 2008 15:40 GMT
How do I find out if a variable is even?
Thanks
Mike H - 18 Mar 2008 15:50 GMT
Use the MOD function

myvariable = 9
If (myvariable Mod 2) > 0 Then
MsgBox "Odd"
Else
MsgBox "Even"
End If

Mike

> How do I find out if a variable is even?
> Thanks
ranswert - 18 Mar 2008 16:08 GMT
Thanks I'll give that a try.

> Use the MOD function
>
[quoted text clipped - 9 lines]
> > How do I find out if a variable is even?
> > Thanks
Dana DeLouis - 18 Mar 2008 19:29 GMT
>> > How do I find out if a variable is even?

Just make sure you are working with Integers since vba's MOD rounds the
input.

Sub xxx()
 Dim n
 n = 9.5

 'Considered Even
 If (n Mod 2) > 0 Then
   MsgBox "Odd"
 Else
   MsgBox "Even"
 End If

 'Odd
 MsgBox WorksheetFunction.IsOdd(9.999999999)
 'Even
 MsgBox WorksheetFunction.IsOdd(9.9999999999)
End Sub

Signature

HTH   :>)
Dana DeLouis
Windows XP & Excel 2007

> Thanks I'll give that a try.
>
[quoted text clipped - 11 lines]
>> > How do I find out if a variable is even?
>> > Thanks
Rick Rothstein (MVP - VB) - 19 Mar 2008 05:15 GMT
You can also do it this way (execution should be marginally faster)...

MyVariable = 9
If MyVariable And 1 Then
 MsgBox "Odd"
Else
 MsgBox "Even"
End If

Rick

> Use the MOD function
>
[quoted text clipped - 9 lines]
>> How do I find out if a variable is even?
>> Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.