Results 1 to 5 of 5

VBA homework help needed

Threaded View

ptpair VBA homework help needed 09-21-2009, 10:29 PM
shg Re: VBA homework help needed 09-21-2009, 11:05 PM
ptpair Re: VBA homework help needed 09-22-2009, 07:30 PM
shg Re: VBA homework help needed 09-22-2009, 08:00 PM
ptpair Re: VBA homework help needed 09-22-2009, 08:20 PM
  1. #1
    Registered User
    Join Date
    09-21-2009
    Location
    t-town
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA homework help needed

    I am having trouble getting my code to work for my cs class and I really need some help. Can someone please look at my code and tell me what I am doing wrong. The instructions are below the code. Thanks
    Dim X As Integer, Y As Integer, Average As Double, Size As Integer, Sum As Integer
    
        Size = InputBox("Enter the number of random numbers to be generated between 20 and 100", "Input")
    
        X = 1: Y = 1
    
        While X <= Size
    
            Sheet1.Cells(X, 1) = Round(20 + Rnd() * 80, 0)
    
            Sum = Sum + Sheet1.Cells(X, 1)
    
            X = X + 1
    
        Wend
    
        Range("c1").Value = "Average="
    
        Range("d1").Value = Sum / Size
    
        Average = Round(Range("d1").Value, 0)
    
        Range("d1").Value = Average
    
        While Y <= Size
    
            If compare(Sheet1.Cells(Y, 1), Average) = 1 Then
    
                Sheet1.Cells(Y, 5) = " The value " & Sheet1.Cells(Y, 1) & " is greater than the average"
    
            ElseIf compare(Sheet1.Cells(Y, 1), Average) = 2 Then
    
                Sheet1.Cells(Y, 5) = " The value " & Sheet1.Cells(Y, 1) & " is  equal to the average"
    
            Else
    
                Sheet1.Cells(Y, 5) = " The value " & Sheet1.Cells(Y, 1) & " is less than the average"
    
            End If
    
            Y = Y + 1
    
        Wend
    Instructions:
    Create a macro called Calculations that will perform the following tasks:
    1. Prompt the user to enter the number of random numbers between 20 and 100 to be generated.
    2. Use a While/Wend loop to generate the random numbers and write them to column A.
    3. Calculate average of the random numbers and write a label in C1 and the average in D1.
    4. Create a function called Compare that has two arguments : The random number and the average and returns an integer. The function will test three conditions:
    1. Number is greater than average, return a 1.
    2. Number is equal to average, return a 2.
    3. Number is less than the average, return a 3.
    5. Since each number must be passed to Compare, a While/Wend loop should be constructed first.
    6. With in the body of the loop, an If/Then/ElseIf statement will be required to invoke the function, pass random number, pass average, and determine if a 1, 2, or 3 was returned from Compare.
    7. If the result is a 1, a label stating the number is greater than the average should be written to the sheet. If the result is 2, a label stating the number is equal to the average should be written to the sheet. If the result is 3, a label stating the number is less than the average should be written to the sheet.
    8. Create a Clear Module that will clear the spreadsheet.
    Last edited by Leith Ross; 09-21-2009 at 11:00 PM. Reason: Added Code Tags - First programming lesson: Read the Rules

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