Creating a simple game in Excel using VBA can be fun and educational. Here,
we’ll create a simple “Guess the Number” game. The player has to
guess a randomly generated number between 1 and 100. Here’s a step-by-step
guide:
Step 1: Set Up the Excel Workbook
1. Open Excel and create a new workbook.
2. Save the workbook as a macro-enabled workbook (.xlsm).
Step 2: Open the VBA Editor
1. Press “ALT + F11” to open the VBA editor.
2. In the VBA editor, insert a new module:
– Click “Insert” > “Module”.
Step 3: Write the VBA Code
Copy and paste the following VBA code into the module:
Sub GuessTheNumber()
Dim secretNumber As Integer
Dim guess As Integer
Dim attempts As Integer
‘ Initialize the game
secretNumber = Int((100 * Rnd) + 1)
attempts = 0
‘ Game loop
Do
guess = Application.InputBox(“Guess a number between 1 and 100:”,
“Guess the Number”, Type:=1)
If guess = False Then Exit Sub ‘ Exit if the user cancels
attempts = attempts + 1
If guess < secretNumber Then
MsgBox “Too low! Try again.”
ElseIf guess > secretNumber Then
MsgBox “Too high! Try again.”
Else
MsgBox “Congratulations! You guessed the number in ” & attempts
& ” attempts.”
Exit Do
End If
Loop
End Sub
Let’s break down the VBA code for the “Guess the Number” game step
by step:
Step-by-Step Description:
1. Sub Declaration:
###VBA###
Sub GuessTheNumber()
######
This declares a new subroutine named “GuessTheNumber”.
2. Variable Declarations:
###VBA###
Dim secretNumber As Integer
Dim guess As Integer
Dim attempts As Integer
######
Here, we declare three variables:
– “secretNumber” (Integer): The number the player needs to guess.
– “guess” (Integer): The player’s current guess.
– “attempts” (Integer): The count of how many guesses the player has made.
3. Initialize the Game:
###VBA###
secretNumber = Int((100 * Rnd) + 1)
attempts = 0
######
– “secretNumber” is assigned a random integer between 1 and 100. “Rnd”
generates a random number between 0 and 1, which is then scaled to a number
between 1 and 100 and converted to an integer using “Int”.
– “attempts” is initialized to “0”, representing the start of the game with no
guesses made yet.
4. Game Loop:
###VBA###
Do
######
The “Do” loop starts here, which will continue until the player guesses the
correct number or exits the game.
5. Prompt for Guess:
###VBA###
guess = Application.InputBox(“Guess a number between 1 and 100:”,
“Guess the Number”, Type:=1)
If guess = False Then Exit Sub ‘ Exit if the user cancels
######
– “Application.InputBox” displays a dialog box asking the player to guess a
number between 1 and 100. The “Type:=1” argument ensures that the input is a
number.
– If the user cancels the input box, “guess” is set to “False”, and the
subroutine exits using “Exit Sub”.
6. Increment Attempt Counter:
###VBA###
attempts = attempts + 1
######
This increments the “attempts” counter by 1 each time the player makes a guess.
7. Check Guess:
###VBA###
If guess < secretNumber Then
MsgBox “Too low! Try again.”
ElseIf guess > secretNumber Then
MsgBox “Too high! Try again.”
Else
MsgBox “Congratulations! You guessed the number in ” & attempts
& ” attempts.”
Exit Do
End If
######
– If “guess” is less than “secretNumber”, a message box displays “Too low!
Try again.”
– If “guess” is greater than “secretNumber”, a message box displays “Too
high! Try again.”
– If “guess” equals “secretNumber”, a message box displays
“Congratulations! You guessed the number in X attempts.” (“X” being
the value of “attempts”). The “Exit Do” statement breaks out of the loop,
ending the game.
8. End Loop:
###VBA###
Loop
######
The loop will continue to prompt for guesses until the correct number is
guessed or the player exits.
9. End Sub:
###VBA###
End Sub
######
This ends the “GuessTheNumber” subroutine.
Summary
This VBA subroutine creates a simple number guessing game. The player is asked
to guess a randomly generated number between 1 and 100, and is given feedback
if the guess is too high or too low. The game continues until the correct
number is guessed or the player cancels the input prompt. The number of
attempts is tracked and displayed when the player guesses correctly.
.. and here the link to first video (this post as video) on my newborn YouTube channel
https://youtu.be/Cj9bmiWJiao
Leave a comment