Consulting

Results 1 to 6 of 6

Thread: Solved: if odd even numbers

  1. #1

    Solved: if odd even numbers

    Ok so I am trying to write a If statement that if a variable is an odd number then it will use one formulae and if it is an even number it will use a different one.
    I tried using
    [VBA]
    If variable Is Odd Then
    'formula 1
    Else
    'formula2
    End If
    [/VBA]
    however Odd/Even aren't useable criteria, do you guys know how I could solve this or if there is something i could use instead of odd?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    [vba]

    If myVar Mod 2 = 1 Then

    MsgBox "odd"
    Else

    MsgBox "even"
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks Xld youve done it again

  4. #4
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Hi,

    [VBA]Sub OddorEven()
    If Var Mod 2 = 0 Then
    'use formula 1
    Else
    'use formula 2
    End If
    End Sub
    [/VBA]

    Hope this helps.

    Regards


    kp

  5. #5
    VBAX Newbie
    Joined
    Sep 2016
    Posts
    2
    Location

    Post

    Sub Button2_click()
    Dim x As Integer
    Dim y As Integer
    x = InputBox("Please enter the Number")
    If x Mod 2 Then
    MsgBox "The Number is the Odd Number"
    Else
    MsgBox "The Number is the Even Number"
    End If
    End Sub
    Last edited by Aussiebear; 09-06-2016 at 02:42 AM. Reason: Added code tags to post and tidied up submitted post

  6. #6
    VBAX Newbie
    Joined
    Sep 2016
    Posts
    2
    Location
    Public Function IsEven(ByVal Number As Long) As Boolean
        IsEven = (Number Mod 2 = 0)
    End Function
    
    Public Sub findEvenOdd()
    Dim x As Integer
    x = InputBox("Please enter the number")
    If IsEven(x) Then
    MsgBox "The Number is the Even Number"
    Else
    MsgBox "The Number is the Odd Number"
    End If
    End Sub
    Last edited by Aussiebear; 09-06-2016 at 02:43 AM. Reason: Added code tags to post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •