By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Odd, Even

P: 6
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:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Public Sub Problem4()
  3.     Dim Var1 As Long, Var2 As Long, Var3 As Long
  4.         'Input 3 numbers from user
  5.         Var1 = InputBox("Enter the first integer :", "First")
  6.         Var2 = InputBox("Enter the second integer :", "Second")
  7.         Var3 = InputBox("Enter the third integer :", "Third")
  8.         Range("a1").Value = "First number ="
  9.         Range("a2").Value = "Second number ="
  10.         Range("a3").Value = "Third number ="
  11.         Range("b1").Value = Var1
  12.         Range("b2").Value = Var2
  13.         Range("b3").Value = Var3
  14.         'Find the Largest Number
  15.         If Var1 > Var2 And Var1 > Var3 Then
  16.             Range("a4").Value = "The largest number is " & Var1
  17.         ElseIf Var2 > Var1 And Var2 > Var3 Then
  18.             Range("a4").Value = "The largest number is " & Var2
  19.         ElseIf Var3 > Var1 And Var3 > Var2 Then
  20.             Range("a4").Value = "The largest number is " & Var3
  21.         End If
  22.         'Find the Smallest Number
  23.         If Var1 < Var2 And Var1 < Var3 Then
  24.             Range("a5").Value = "The smallest number is " & Var1
  25.         ElseIf Var2 < Var1 And Var2 < Var3 Then
  26.             Range("a5").Value = "The smallest number is " & Var2
  27.         ElseIf Var3 < Var1 And Var3 < Var2 Then
  28.             Range("a5").Value = "The smallest number is " & Var3
  29.         End If
  30.         If Var1 Mod 2 = 0 Then
  31.             Range("a6").Value = "This is an even number" & Var1
  32.         Else = "This is an odd number" & Var1
  33.            End If
  34.  
  35.  
  36.         Columns("a").EntireColumn.AutoFit
  37. End Sub
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.
Sep 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: 21
Expand|Select|Wrap|Line Numbers
  1.    Else = "This is an odd number" & Var1
  2.  

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 :-)
Sep 5 '07 #2

QVeen72
Expert 100+
P: 1,445
Hi,

Change it to :

Expand|Select|Wrap|Line Numbers
  1. If Var1 Mod 2 = 0 Then
  2.      Range("a6").Value = "This is an even number" & Var1
  3. Else 
  4.      Range("a6").Value = "This is an odd number" & Var1
  5. End If
  6.  
  7.  
REgards
Veena
Sep 5 '07 #3

P: 21
Hi,

Change it to :

Expand|Select|Wrap|Line Numbers
  1. If Var1 Mod 2 = 0 Then
  2.      Range("a6").Value = "This is an even number" & Var1
  3. Else 
  4.      Range("a6").Value = "This is an odd number" & Var1
  5. End If
  6.  
  7.  
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.
Sep 5 '07 #4

Expert 5K+
P: 8,434
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.
Sep 6 '07 #5

P: 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.
Sep 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.