VBA guessing game

Alcat03

New Member
Joined
Nov 7, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
hello so I trying to make this guessing game and i have all the code written down but the last part. When i tired it did not work. Can someone show me how to add the
If their guess is within two of the mystery number, do not tell them if their guess was too high/low but output “Close but no cigar. Try again."

Have the computer randomly select a number between 1 and 100 using a “Reset”
button. Create a separate button “Guess” to prompt the user to guess the number. Let the player
know if their guess is too high or too low. If their guess is within two of the mystery number, do not
tell them if their guess was too high/low but output “Close but no cigar. Try again

code:
Sub Button_Reset()
Row = 1
Cells.Clear
mystery = Application.RandBetween(1, 100)
MsgBox ("The computer has chosen a number between 1 and 100. Click on Try to guess it")



End Sub
Sub Button2_Click()
Dim guess As Integer
guess = InputBox("Enter a number between 1 and 100")

If guess < mystery Then
MsgBox ("Too low. Click 'guess' to try again.")
Cells(Row, 1) = guess
Cells(Row, 2) = "Too Low"
ElseIf guess > mystery Then
MsgBox ("Too high. Click 'Guess' to try again")
Cells(Row, 1) = guess
Cells(Row, 2) = "too High"
Else: guess = mystery
MsgBox ("you have guessed it! The mystery number is " & mystery & ".")
Cells(Row, 1) = guess
Cells(Row, 2) = "you Win"
End If
Row = Row + 1
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
VBA Code:
If abs(guess-mystery)<3 and guess<>mystery Then...

You will also need to declare mystery as a Public variable outside the subs or you mystery won't have a value when you run the Button2_Click procedure
 
Upvote 0
You also shouldn't use Row as a variable name as it is a reserved word in VBA. Also, it is good practice to not repeat code over and over again. You also will need what I called iRow as Public so you can use it again in the next iteration of the procedure.
VBA Code:
Public mystery As Integer, iRow As Integer
Sub Button_Reset()
Row = 1
Cells.Clear
mystery = Application.RandBetween(1, 100)
MsgBox ("The computer has chosen a number between 1 and 100. Click on Try to guess it")
iRow = 1
End Sub
Sub Button2_Click()
Dim guess As Integer, msg As String
guess = InputBox("Enter a number between 1 and 100")

If guess = mystery Then
    MsgBox "You have guessed it! The mystery number is " & mystery & "."
    Exit Sub
ElseIf Abs(guess - mystery) < 3 Then
    msg = "Close but no cigar."
ElseIf guess < mystery Then
    msg = "Too low."
ElseIf guess > mystery Then
    msg = "Too high."
End If
   
Cells(iRow, 1) = guess
Cells(iRow, 2) = msg

MsgBox msg & " Click 'guess' to try again"

iRow = iRow + 1
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top