+ Reply to Thread
Results 1 to 4 of 4

Need help on VBA code for school project!

  1. #1
    Registered User
    Join Date
    11-04-2011
    Location
    Tuscaloosa
    MS-Off Ver
    Excel 2007
    Posts
    3

    Need help on VBA code for school project!

    I have an assignment for school I need to get done and I cannot figure out the right codes for this for the life of me. This is my first course in VBA and I really need some help badly! Heres the project:

    CS385: Project 1

    Check Calendar for Due Date





    Create a macro called Calculations that will perform the following tasks:



    1. Prompt the user to enter three numbers and store them in appropriate variables.



    2. Create a private function Min which will take the three variables as arguments. The procedure will determine the smallest of the three numbers entered by the user and return it to a variable named Smallest using an assignment statement.



    3. Create a private function Max which will take three variables as arguments. The procedure will determine the largest of the three numbers entered by the user and return it to a variable named Largest using an assignment statement.



    4. Determine if each variable is even or odd and print the variable and result to the spreadsheet.



    5. Output the following with labels starting in cell A1:

    a. All three numbers entered by the user.

    b. Smallest of numbers entered by the user.

    c. Largest of numbers entered by the user.

    g. Numbers are Even or Odd

    h. Column A should be resized using code to the widest statement



    6. Save the workbook in this form CollegeJoeHW1.xls and submit it to WebCT.


    Sample Output Results:


    Column A in Excel Column B in Excel
    First number= 46
    Second number= 68
    Third number= 23
    The largest nuber is 68
    The smallest number is 23
    The number 45 is odd
    The number 68 is even
    The number 23 is odd


    *He gave us a starter code for it:

    This is some code to get you started on Project 1.

    Option Explicit

    Public Sub Calculation()

    'Define variables

    Dim Var1 As Long, Var2 As Long, Var3 As Long, Largest As Integer, Smallest As Integer

    '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")

    'Write information to the spreadsheet

    Range("a1").Value = "First number = "

    Range("a2").Value = "Second number = "

    Range("a3").Value = "Third number = "

    Range("b1").Value = Var1

    Range("b2").Value = Var2

    Range("b3").Value = Var3

    'Find the Largest Number

    Largest = Max(Var1, Var2, Var3)

    Range("a4").Value = "The largest number is " & Largest

    'Find the Smallest Number

    Smallest = Min(Var1, Var2, Var3)

    Range("a5").Value = "The smallest number is " & Smallest

    If Var1 Mod 2 = 0 Then

    Range("a6").Value = "The number " & Var1 & " is even"

    Else

    Range("a6").Value = "The number " & Var1 & " is odd"

    End If

    If Var2 Mod 2 = 0 Then

    Range("a7").Value = "The number " & Var2 & " is even"

    Else

    Range("a7").Value = "The number " & Var2 & " is odd"

    End If

    If Var3 Mod 2 = 0 Then

    Range("a8").Value = "The number " & Var3 & " is even"

    Else

    Range("a8").Value = "The number " & Var3 & " is odd"

    End If

    Columns("a:a").EntireColumn.AutoFit

    End Sub

    You will need to create the Max Function and Min Function in the following form:

    Private Function Max(Var1 As Integer, Var2 As Integer, Var3 As Integer) As Integer

    ‘Place your If statements here from Computer Exercise 2, Problem 4 which finds the largest and smallest values from three integers
    End Function

    *Then he gave us another function code to check if we were right. He gave us the Min and Max function:

    Private Function Min(v1 As Long, v2 As Long, v3 As Long) As Long

    If v1 < v2 And v1 < v3 Then

    Min = v1

    ElseIf v2 < v1 And v2 < v3 Then

    Min = v2

    ElseIf v3 < v1 And v3 < v2 Then

    Min = v3

    End If

    End Function

    Private Function Max(v1 As Long, v2 As Long, v3 As Long) As Long

    If v1 > v2 And v1 > v3 Then

    Max = v1

    ElseIf v2 > v1 And v2 > v3 Then

    Max = v2

    ElseIf v3 > v1 And v3 > v2 Then

    Max = v3

    End If
    End Function





    *If anyone give me the correct code so I can figure out my mistakes please. I would be very thankful!!

  2. #2
    Forum Contributor
    Join Date
    09-08-2011
    Location
    Leicester, England
    MS-Off Ver
    Excel 2007
    Posts
    157

    Re: Need help on VBA code for school project!

    All code must be put in the [Code] [Code/] tags as all that is unclear and hard to read, people will be more inclined to help you if you follow the rules

  3. #3
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Need help on VBA code for school project!

    Sorry, it is forum policy not to provide help for school projects.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Need help on VBA code for school project!

    Hi,

    Rather than just copying and pasting what you've been given try to show where you are having difficulty and demonstrate that you have made an effort to answer the problem set.

    People will be more inclined to help if you show that you've tried.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1