Odd, Even

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coreyover4
    New Member
    • Sep 2007
    • 6

    Odd, Even

    I am new just now taking classes on excel and using VB and VBA. Anyways I am having difficulties doing a code. Right now I have:

    [CODE=vb]Option Explicit
    Public Sub Problem4()
    Dim Var1 As Long, Var2 As Long, Var3 As Long
    'Input 3 numbers from user
    Var1 = InputBox("Enter the first integer :", "First")
    Var2 = InputBox("Enter the second integer :", "Second")
    Var3 = InputBox("Enter the third integer :", "Third")
    Range("a1").Val ue = "First number ="
    Range("a2").Val ue = "Second number ="
    Range("a3").Val ue = "Third number ="
    Range("b1").Val ue = Var1
    Range("b2").Val ue = Var2
    Range("b3").Val ue = Var3
    'Find the Largest Number
    If Var1 > Var2 And Var1 > Var3 Then
    Range("a4").Val ue = "The largest number is " & Var1
    ElseIf Var2 > Var1 And Var2 > Var3 Then
    Range("a4").Val ue = "The largest number is " & Var2
    ElseIf Var3 > Var1 And Var3 > Var2 Then
    Range("a4").Val ue = "The largest number is " & Var3
    End If
    'Find the Smallest Number
    If Var1 < Var2 And Var1 < Var3 Then
    Range("a5").Val ue = "The smallest number is " & Var1
    ElseIf Var2 < Var1 And Var2 < Var3 Then
    Range("a5").Val ue = "The smallest number is " & Var2
    ElseIf Var3 < Var1 And Var3 < Var2 Then
    Range("a5").Val ue = "The smallest number is " & Var3
    End If
    If Var1 Mod 2 = 0 Then
    Range("a6").Val ue = "This is an even number" & Var1
    Else = "This is an odd number" & Var1
    End If


    Columns("a").En tireColumn.Auto Fit
    End Sub[/CODE]

    I am fine up to the point of the if Var1 Mod 2=0 part. I have to display in excel after the calculation if the number is odd or even and I cant seem to get the right code to work. I have to do this for all three variables. Any help with writing the code would be appreciated. If you take out the if var1 and run the macro then everything works out fine. So all i need is the even/odd help.
    Last edited by Killer42; Sep 5 '07, 09:12 AM. Reason: Added [CODE=vb] tag
  • Martin2007
    New Member
    • Jul 2007
    • 21

    #2
    [CODE=vb] Else = "This is an odd number" & Var1
    [/CODE]


    The code fragment I have highlighted is where I think the problem is.... is this a school project, if so then I cannot really help you any more than to point you in the right direction :-)

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      Change it to :

      [code=vb]
      If Var1 Mod 2 = 0 Then
      Range("a6").Val ue = "This is an even number" & Var1
      Else
      Range("a6").Val ue = "This is an odd number" & Var1
      End If

      [/code]

      REgards
      Veena

      Comment

      • Martin2007
        New Member
        • Jul 2007
        • 21

        #4
        Originally posted by QVeen72
        Hi,

        Change it to :

        [code=vb]
        If Var1 Mod 2 = 0 Then
        Range("a6").Val ue = "This is an even number" & Var1
        Else
        Range("a6").Val ue = "This is an odd number" & Var1
        End If

        [/code]

        REgards
        Veena
        I wasn't going to tell him exactly that as it is a homework task... but yes, that is what I had spotted.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by Martin2007
          I wasn't going to tell him exactly that as it is a homework task... but yes, that is what I had spotted.
          You can find the homework/coursework policy here. I recommend everyone familiarise themselves with it. But what it really boils down to is this:
          We won't do your homework for you ("you" being the student). But if you are making a real effort and run into difficulties, we don't mind helping you to understand the way forward. After all, if you're really stuck you need to ask someone.

          Personally I'm satisfied that coreyover4 has done most of the work, though I also would have tried to prod him/her in the right direction rather than providing a ready-made answer.

          Comment

          • coreyover4
            New Member
            • Sep 2007
            • 6

            #6
            Thanks for the help. I didnt want the whole answer, I just wanted to get pointed in the right direction on where my code was wrong and how to fix it. Thanks and I appreciate the help.

            Comment

            Working...