| 
View
 

Guessing Game (VBA using Excel)

Page history last edited by PBworks 18 years, 5 months ago

Public Sub GuessingGame()

Dim Secret As Integer

Dim Guess As Integer

Dim Tries As Integer

 

Randomize  'Inittializes random-number generator

 

Secret = Int((100 * Rnd) + 1) 'Generates a random number between 1 and 100

 

Guess = 0

Tries = 0

 

Do While Guess <> Secret

Guess = InputBox("Guess a number bewteen 1 and 100.")

If Guess = Secret Then

    Tries = Tries + 1

    MsgBox ("Well done! You guessed the number.")

    MsgBox ("It took you " & Tries & " to guess the number")

ElseIf Guess > Secret Then

    Tries = Tries + 1

    MsgBox ("Wrong. Too high. Try again.")

Else

    Tries = Tries + 1

    MsgBox ("Wrong. Too low. Try again.")

End If

Loop

End Sub

Comments (0)

You don't have permission to comment on this page.