Frein
05-28-2008, 05:01 AM
I'm working on an assignment for an advanced Excel class and I've hit a brick wall on this macro I'm trying to create. The macro is supposed to ask for a number between 1 and 100 and then make you guess what was inputed previously until you get it right.
My problem is I can't make the script recognize the correct answer. I'll post the problematic part of my code and hopefully someone can point out the flaw in it. Note that I'm an absolute novice at this stuff and it may look messy.
'Input the first number
Private Sub CommandButton3_Click()
Dim Minimum As Integer
Dim Maximum As Integer
Dim UserInput As String
Dim Message As String
Dim GoodNumber As Boolean
Minimum = 1
Maximum = 100
Goodnumber = False
Message = "Input a number between " & Minimum & " and " & Maximum
Do Until GoodNumber = True
UserInput = InputBox(Message)
If UserInput = "" Then
Exit Sub
End If
If IsNumeric(UserInput) Then
If UserInput >= Minimum And UserInput <= Maximum Then
GoodNumber = True
Else
Message = "Your latest input was invalid."
Message = Message & vbNewLine
Message = Message & "Input a number between " & Minimum & " and " & Maximum
End If
Else
Message = "Your latest input was invalid."
Message = Message & vbNewLine
Message = Message & "Input a number between " & Minimum & " and " & Maximum
End If
Loop
'Guess the correct number
Dim UserInput2 As String
Dim CorrectNumber As Boolean
Dim Message2 As String
CorrectNumber = False
Message2 = "Guess the correct number."
Do Until CorrectNumber = True
UserInput2 = InputBox(Message2)
If UserInput2 = "" Then
Exit Sub
End If
If IsNumeric(UserInput2) Then
If UserInput2 = UserInput Then
CorrectNumber = True
MsgBox ("Correct!")
Else
Message2 = "You guessed wrong."
Message2 = Message2 & vbNewLine
Message2 = Message2 & "Guess the correct number."
End If
Else
Message2 = "Your latest input was invalid."
Message2 = Message2 & vbNewLine
Message2 = Message2 & "Guess the correct number."
End If
Loop
End Sub
Basically, I can't make the Boolean CorrectNumber become True even if according to my logic this is how it should work. I tested it with If UserInput2 = Minimum and got it to work by guessing 1, but it won't recognize UserInput as a reference like it does pre-defined variables, such as Minimum or Maximum. I'd really appreciate any help with his.
My problem is I can't make the script recognize the correct answer. I'll post the problematic part of my code and hopefully someone can point out the flaw in it. Note that I'm an absolute novice at this stuff and it may look messy.
'Input the first number
Private Sub CommandButton3_Click()
Dim Minimum As Integer
Dim Maximum As Integer
Dim UserInput As String
Dim Message As String
Dim GoodNumber As Boolean
Minimum = 1
Maximum = 100
Goodnumber = False
Message = "Input a number between " & Minimum & " and " & Maximum
Do Until GoodNumber = True
UserInput = InputBox(Message)
If UserInput = "" Then
Exit Sub
End If
If IsNumeric(UserInput) Then
If UserInput >= Minimum And UserInput <= Maximum Then
GoodNumber = True
Else
Message = "Your latest input was invalid."
Message = Message & vbNewLine
Message = Message & "Input a number between " & Minimum & " and " & Maximum
End If
Else
Message = "Your latest input was invalid."
Message = Message & vbNewLine
Message = Message & "Input a number between " & Minimum & " and " & Maximum
End If
Loop
'Guess the correct number
Dim UserInput2 As String
Dim CorrectNumber As Boolean
Dim Message2 As String
CorrectNumber = False
Message2 = "Guess the correct number."
Do Until CorrectNumber = True
UserInput2 = InputBox(Message2)
If UserInput2 = "" Then
Exit Sub
End If
If IsNumeric(UserInput2) Then
If UserInput2 = UserInput Then
CorrectNumber = True
MsgBox ("Correct!")
Else
Message2 = "You guessed wrong."
Message2 = Message2 & vbNewLine
Message2 = Message2 & "Guess the correct number."
End If
Else
Message2 = "Your latest input was invalid."
Message2 = Message2 & vbNewLine
Message2 = Message2 & "Guess the correct number."
End If
Loop
End Sub
Basically, I can't make the Boolean CorrectNumber become True even if according to my logic this is how it should work. I tested it with If UserInput2 = Minimum and got it to work by guessing 1, but it won't recognize UserInput as a reference like it does pre-defined variables, such as Minimum or Maximum. I'd really appreciate any help with his.