Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default even numbers

How to check in VBA code if number is even (or odd)?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default even numbers


"Tommi" wrote in message
...
How to check in VBA code if number is even (or odd)?


Think of a number, divide it by 2, take the integer part, and multiply that
by 2. Then take away the number you first thought of. If you are left with
nothing then the number you started with was even.

HTH
Geoff




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default even numbers

Try this Tommi

Sub test()
If ActiveCell.Value Mod 2 = 0 Then
MsgBox "Even"
Else
MsgBox "odd"
End If
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Tommi" wrote in message ...
How to check in VBA code if number is even (or odd)?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default even numbers

Use the Mod operator.

dim EvenOdd as string

if(x mod y =0)then
EvenOdd="Even"
else
EvenOdd="Odd"
End If

The mod operator divides two numbers and returns the
remainder. If the remainder is 0 it is even.

Hope that helps!

Kevin
-----Original Message-----

"Tommi" wrote in message
...
How to check in VBA code if number is even (or odd)?


Think of a number, divide it by 2, take the integer part,

and multiply that
by 2. Then take away the number you first thought of. If

you are left with
nothing then the number you started with was even.

HTH
Geoff




.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 77
Default even numbers

Just another option. If you have a vba library reference set to
atpvbaen.xls, then you can use IsOdd / IsEven directly.

Sub Demo()
Debug.Print IsOdd(12345678901234)
Debug.Print IsEven(12345678901234)

Debug.Print IsOdd(12345678901235)
Debug.Print IsEven(12345678901235)
End Sub

returns...
False
True
True
False
--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Tommi" wrote in message
...
How to check in VBA code if number is even (or odd)?






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default even numbers

I tried this. I set the Tools/Referencese to atpvbaen.xls and then used
IsEven. but I get this "Run-Time Error '1004' The macro " cannot be found"
What is wrong?

"Dana DeLouis" wrote in message
...
Just another option. If you have a vba library reference set to
atpvbaen.xls, then you can use IsOdd / IsEven directly.

Sub Demo()
Debug.Print IsOdd(12345678901234)
Debug.Print IsEven(12345678901234)

Debug.Print IsOdd(12345678901235)
Debug.Print IsEven(12345678901235)
End Sub

returns...
False
True
True
False
--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Tommi" wrote in message
...
How to check in VBA code if number is even (or odd)?






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 77
Default even numbers

I'm not sure. If I don't have the reference set, then I get a
"Compile error: Sub or Function not defined"

I'm just guessing. Unselect this in the vba library. Go to the worksheet,
and select <Tools <Add-Ins and make sure the Analysis ToolPak is selected.
Then, try the library reference again.

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Tommi" wrote in message
...
I tried this. I set the Tools/Referencese to atpvbaen.xls and then used
IsEven. but I get this "Run-Time Error '1004' The macro " cannot be found"
What is wrong?

"Dana DeLouis" wrote in message
...
Just another option. If you have a vba library reference set to
atpvbaen.xls, then you can use IsOdd / IsEven directly.

Sub Demo()
Debug.Print IsOdd(12345678901234)
Debug.Print IsEven(12345678901234)

Debug.Print IsOdd(12345678901235)
Debug.Print IsEven(12345678901235)
End Sub

returns...
False
True
True
False
--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Tommi" wrote in message
...
How to check in VBA code if number is even (or odd)?








  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default even numbers

Thanks for your mail Dana!
This is what I did, and now it works:
I checked Tools/Add-Ins/Analysis Toolpak - VBA in Excel.
Then I checked Tools/References/atpvbaen.xls in Visual Basic Editor.
(for you with similar problem: if you don't see atpvbaen.xls in Visual Basis
Editor Tools/References,
click Browse and find file atpvbaen.xla - probably in \Microsoft Office\
Office10\Library, or something like that)

Regards,
Tommi

"Dana DeLouis" wrote in message
...
I'm not sure. If I don't have the reference set, then I get a
"Compile error: Sub or Function not defined"

I'm just guessing. Unselect this in the vba library. Go to the

worksheet,
and select <Tools <Add-Ins and make sure the Analysis ToolPak is

selected.
Then, try the library reference again.

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Tommi" wrote in message
...
I tried this. I set the Tools/Referencese to atpvbaen.xls and then used
IsEven. but I get this "Run-Time Error '1004' The macro " cannot be

found"
What is wrong?

"Dana DeLouis" wrote in message
...
Just another option. If you have a vba library reference set to
atpvbaen.xls, then you can use IsOdd / IsEven directly.

Sub Demo()
Debug.Print IsOdd(12345678901234)
Debug.Print IsEven(12345678901234)

Debug.Print IsOdd(12345678901235)
Debug.Print IsEven(12345678901235)
End Sub

returns...
False
True
True
False
--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Tommi" wrote in message
...
How to check in VBA code if number is even (or odd)?










Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel, change column of negative numbers to positive numbers? Nita New Users to Excel 3 November 27th 07 04:54 AM
Excel Formula - Add column of numbers but ignore negative numbers view for Distribution List members Excel Worksheet Functions 1 April 7th 06 03:13 AM
Help! How do you get excel to find the x(changes daily, marked in a cell from another formula) highest numbers in a group of numbers and sum them up? C-Man23 Excel Worksheet Functions 3 January 19th 06 09:52 AM
Help! How do you get excel to find the x(changes daily, marked in a cell from another formula) highest numbers in a group of numbers and sum them up? C-Man23 Excel Worksheet Functions 1 January 9th 06 01:23 PM
Count comma separated numbers, numbers in a range with dash, not t Mahendra Excel Discussion (Misc queries) 0 August 8th 05 05:56 PM


All times are GMT +1. The time now is 06:53 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"