Guess my number game in VBA - incorrect response to higher value input for first answer

Chelsea0270516

New Member
Joined
Oct 15, 2015
Messages
32
Hello! I am just working through making this (primarily for fun) and have encountered a weird error - this is a guess the random number game. The code generates a random number & assigns it to the variable myNumber. The user is prompted to guess the number & their guess is assigned to the variable userGuess.

What is weird is no matter what the first userGuess is in relation to the myNumber the answer is always "Higher!" Any guesses after the first one work the way they should.

Anyone have any ideas on what could be causing that? (Or a method of testing to figure that out?)

I made the code display myNumber just to confirm that it wasn't changing between guesses & that wasn't the issue.

I also gave a value to userGuess before the prompt & that didn't change the process either.

(This code doesn't have either of those tests included in it)

Code:
Sub DoUntilLoopGuessNumber()    Dim myNumber As Integer
    myNumber = Int(100 * Rnd) + 1 + Int(100 * Rnd)
    InputBox ("Guess my number!")
    Dim userGuess As Integer
    Do Until userGuess = myNumber
        If userGuess > myNumber Then
            MsgBox "Lower!"
        Else
            MsgBox "Higher!"
        End If
        userGuess = InputBox("Guess my number!")
    Loop
    MsgBox "Congrats!  You guessed it!"
    
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try
Code:
Sub DoUntilLoopGuessNumber()
Dim myNumber As Integer, userGuess As Integer
myNumber = Int(100 * Rnd) + 1 + Int(100 * Rnd)
userGuess = InputBox("Guess my number!")
    Do Until userGuess = myNumber
        If userGuess > myNumber Then
            MsgBox "Lower!"
        Else
            MsgBox "Higher!"
        End If
    userGuess = InputBox("Guess my number!")
    Loop
    MsgBox "Congrats!  You guessed it!"
    
End Sub
 
Upvote 0
Hello! I am just working through making this (primarily for fun) and have encountered a weird error - this is a guess the random number game. The code generates a random number & assigns it to the variable myNumber. The user is prompted to guess the number & their guess is assigned to the variable userGuess.

What is weird is no matter what the first userGuess is in relation to the myNumber the answer is always "Higher!" Any guesses after the first one work the way they should.

Anyone have any ideas on what could be causing that? (Or a method of testing to figure that out?)

I made the code display myNumber just to confirm that it wasn't changing between guesses & that wasn't the issue.

I also gave a value to userGuess before the prompt & that didn't change the process either.

(This code doesn't have either of those tests included in it)

Code:
Sub DoUntilLoopGuessNumber()    Dim myNumber As Integer
    myNumber = Int(100 * Rnd) + 1 + Int(100 * Rnd)
    InputBox ("Guess my number!")
    Dim userGuess As Integer
    Do Until userGuess = myNumber
        If userGuess > myNumber Then
            MsgBox "Lower!"
        Else
            MsgBox "Higher!"
        End If
        userGuess = InputBox("Guess my number!")
    Loop
    MsgBox "Congrats!  You guessed it!"
    
End Sub

here you go. Avoid IF when you need multiple selection options on a single comparison.
Code:
Sub GuessNumber()
Dim myNumber As Integer, userGuess As Integer

    myNumber = Int(100 * Rnd) + 1 + Int(100 * Rnd)
    
    Do Until userGuess = myNumber

        userGuess = InputBox("Guess my number!")

        Select Case userGuess
            Case 0
                MsgBox "Aww, sorry to see you quit... maybe next time!"
                Exit Sub
            Case Is > myNumber
                MsgBox "Lower!"
            Case Is < myNumber
                MsgBox "Higher!"
            Case Is = myNumber
                MsgBox "Congrats!  You guessed it!"
        End Select

    Loop
    
End Sub
 
Upvote 0
Try
Code:
Sub DoUntilLoopGuessNumber()
Dim myNumber As Integer, userGuess As Integer
myNumber = Int(100 * Rnd) + 1 + Int(100 * Rnd)
userGuess = InputBox("Guess my number!")
    Do Until userGuess = myNumber
        If userGuess > myNumber Then
            MsgBox "Lower!"
        Else
            MsgBox "Higher!"
        End If
    userGuess = InputBox("Guess my number!")
    Loop
    MsgBox "Congrats!  You guessed it!"
    
End Sub

OH I see it. I didn't have the initial guess set as the input. Thanks!!! :D

Rhodie72 - that looks cool! I haven't used that function yet. My current project is to find uses for the functions I have used/combine them in interesting ways so that they will set in my brain. I was wondering if there was a way to set up a response to no answer as well & you answered that one for me!


 
Upvote 0
OH I see it. I didn't have the initial guess set as the input. Thanks!!! :D

Rhodie72 - that looks cool! I haven't used that function yet. My current project is to find uses for the functions I have used/combine them in interesting ways so that they will set in my brain. I was wondering if there was a way to set up a response to no answer as well & you answered that one for me!

Just to add, Case 0 is actually an answer as opposed to "no answer". The answer is 0 (zero).

Code:
        Select Case userGuess
            Case 0
                MsgBox "Aww, sorry to see you quit... maybe next time!"
                Exit Sub
            Case Is > myNumber
                MsgBox "Lower!"
            Case Is < myNumber
                MsgBox "Higher!"
            Case Is = myNumber
                MsgBox "Congrats!  You guessed it!"
        End Select

There is a Case Else which you could use for a 'catch-all' should you get a case not covered in the Select Case statement.

Code:
        Select Case userGuess
            Case 0
                MsgBox "Aww, sorry to see you quit... maybe next time!"
                Exit Sub
            Case Is > myNumber
                MsgBox "Lower!"
            Case Is < myNumber
                MsgBox "Higher!"
            Case Is = myNumber
                MsgBox "Congrats!  You guessed it!"
            Case Else
               '
        End Select

Howard
 
Upvote 0
@Chelsea0270516

A few comments/suggestions.

1. Whilst 'Integer' is a valid variable type, vba converts Integer variables to Long variables before working with them. Therefore, I would recommend using Long in the first place. (It saves a few characters of typing as well as the wasted conversion process :))

2. As a user, I found not having any idea of how big the number might be, or any obvious way to exit the game if I got sick of it somewhat annoying.

3. I would not take as blanket guidance the statement: "Avoid IF when you need multiple selection options on a single comparison."
If .. Then .. Else .. End If
and
Select Case
both have pros and cons. I would rather say to keep them both in mind and choose the one that best suits the situation and/or your coding comfort/style.



So here is another alternative to consider or use coding ideas from. I'm not suggesting that you necessarily use all aspects of this code, but offer it for possible options.

This one ..
- Guides the user on what range to guess in by constantly updating that information in the InputBox header.
- Displays the information about how to exit if you want.
- Doesn't error if the user accidentally (or deliberately) enters text instead of a number in the Input Box, or clicks 'Cancel' or the 'X' at top right to exit the Input Box.
- Allows you to set the initial random number range by altering the lLow and lHigh starting values in the code.


Rich (BB code):
Sub DoUntilLoopGuessNumberV2()
  Dim myNumber As Long, userGuess As Long, lHigh As Long, lLow As Long
  Dim sMsg As String
  
  Const sExit As String = vbLf & "(Enter 0 to quit)"
  Const sTitle As String = "Guess my number (#L-#H)"
  
  sMsg = "Guess my number!" & sExit
  lLow = 1
  lHigh = 100
  myNumber = lLow + Int(lHigh * Rnd)
  userGuess = Application.InputBox(Prompt:=sMsg, Title:=Replace(Replace(sTitle, "#L", lLow), "#H", lHigh), Type:=1)
  Do Until userGuess = myNumber Or userGuess = 0
    If userGuess > myNumber Then
      sMsg = "Lower!" & sExit
      If userGuess <= lHigh Then lHigh = userGuess - 1
    Else
      sMsg = "Higher!" & sExit
      If userGuess >= lLow Then lLow = userGuess + 1
    End If
    userGuess = Application.InputBox(Prompt:=sMsg, Title:=Replace(Replace(sTitle, "#L", lLow), "#H", lHigh), Type:=1)
  Loop
  If userGuess = 0 Then
    MsgBox "OK, you quit"
  Else
    MsgBox "Congrats!  You guessed it!"
  End If
End Sub
 
Upvote 0
@Chelsea0270516

A few comments/suggestions...

This one .
- Guides the user on what range to guess in by constantly updating that information in the InputBox header.
- Displays the information about how to exit if you want.
- Doesn't error if the user accidentally (or deliberately) enters text instead of a number in the Input Box, or clicks 'Cancel' or the 'X' at top right to exit the Input Box.
- Allows you to set the initial random number range by altering the lLow and lHigh starting values in the code.

Just when I think I've done something good, somebody else trumps my answer and reminds me that I'm still a noob! lol
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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