Thread: VBA Excel
View Single Post
Old 10-15-2010, 06:17 PM   #14
flamesrule_kipper34
Franchise Player
 
flamesrule_kipper34's Avatar
 
Join Date: Aug 2008
Location: Calgary, AB
Exp:
Default

I'm attempting to make a code for the following situation: "Write a subroutine that requests the user to enter a negative even integer using an input box. The subroutine needs to sum all the even integers between one hundred and twenty-one (121) and the inputted number and display the result in a message box. Your message box must let the user know what they are seeing and must include the number entered into the input box as part of the message. This routine must Include error checking that validates the initial inputted number as negative, as odd, and as an integer. You may not use the spreadsheet to accomplish any aspect of the task except to hold the form control button that invokes the subroutine."

Here's what I have so far:

Sub enternumber()
Dim mynum As Integer, Sum As Double, i As Double, isValid As Boolean
mynum = InputBox("Please enter a negative even integer")
Do Until isValid = True
If mynum >= 0 Then
MsgBox ("Please enter a negative even integer")
isValid = False
mynum = InputBox("Please enter a negative even integer")
Else
isValid = True
End If
Loop
Sum = 0

For i = mynum + 1 To 0 Step 2
Sum = Sum + i
Next
MsgBox ("The sum is " & Sum)
End Sub

Last edited by flamesrule_kipper34; 10-15-2010 at 06:44 PM.
flamesrule_kipper34 is offline   Reply With Quote