PDA

View Full Version : Macro to generate random number then User needs to guess that value



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.

Bob Phillips
05-28-2008, 05:14 AM
Seems to work for me.

I input 75 to start with, then entered a guess of 75 (not ucxh of a guess), and it ran through fine.

CreganTur
05-28-2008, 06:12 AM
Works fine for me too.

I ran it through a few different tests:

-At initial input request entered "a"
-Correct error message was launched
-Enterd "900" into input box
-correct error message was launched
-entered "75" into input box
-answer accepted
-guess correct answer input box launched
-entered "2"
-correct error message was launched
-entered 82
-correct error message was launched
-entered "75"
-"correct" message was launched

Why do you think this program isn't working?

Frein
05-29-2008, 05:04 PM
What? It works for you? =/

I tried running it many, many times and it would keep asking for a new number, always failing the one I typed even though I know it was the correct one. I'll have to test it again tomorrow when I get access to a computer with Excel.

Bob Phillips
05-30-2008, 02:08 AM
Having Excel might be useful to test it.

How are you testing it without Excel?

Simon Lloyd
05-30-2008, 02:44 AM
To make sure an input box is numerical you don't need to use the IsNumeric as an input box has it's own set of types (use help in the VBE) to force a number only use:

UserInput= Application.InputBox(Message,"Number Guessing",,,,,,1)
where 1 is the type number for numbers only!

Simon Lloyd
05-30-2008, 02:48 AM
Having Excel might be useful to test it.

How are you testing it without Excel?Does OpenOffice support VBA? or is there stand alone modelling software available?

Oorang
05-30-2008, 04:22 AM
Works ok for me too. If you think it's not, what condition does it fail under?

Frein
05-30-2008, 05:18 AM
Having Excel might be useful to test it.

How are you testing it without Excel?
I work on it on a school computer. I have OpenOffice myself but it uses different objects and I don't even know how to access the VBA editor.


Works ok for me too. If you think it's not, what condition does it fail under?
I just tested it again and now it works. I did translate the messages and variable names, but there is no way I could have missed a mistake while doing so, so I don't understand why it works now in English. Oh well, I'll just leave it like that.

As for how it failed, it used to simply feed me the wrong answer response regardless of what numbers I used.

Charlize
05-30-2008, 05:25 AM
If you don't have excel, try zoho office, the spreadsheet solution. They claim that they added limited vba support. ie, everything but forms, doubleclickevent and buttons on worksheet. But you can perform the type of macro you want to create.

Charlize

CreganTur
05-30-2008, 06:41 AM
:wot Not to be rude... but why are you not developing and testing in Excel if this is for an advanced Excel class?:confused:

Bob Phillips
05-30-2008, 07:58 AM
:wot Not to be rude... but why are you not developing and testing in Excel if this is for an advanced Excel class?:confused:

And if this is advanced, what are the intermediate and basic classes like?

mdmackillop
05-30-2008, 08:07 AM
From an Advanced ECDL course




Macros




AM4.5.1.1 Record a simple macro (e.g. page setup changes.)

AM4.5.1.2 Run a macro.
AM4.5.1.3 Assign a macro to a custom button on a toolbar

CreganTur
05-30-2008, 08:53 AM
From an Advanced ECDL course



If that's an advanced class, then can I go ahead and get my diploma now:clever:

Oorang
05-30-2008, 09:12 AM
j/k



If that's an advanced class, then can I go ahead and get my diploma now:clever:
Yes yes, we get it, "CreganTur smart, whole world dumb." we get it:boring:. Here's your cookie :cookie:, off you go now ;)...

j/k

CreganTur
05-30-2008, 09:20 AM
Yes yes, we get it, "CreganTur smart, whole world dumb." we get it:boring:. Here's your cookie :cookie:, off you go now ;)...


What I said did come out sounding really harsh.

Sorry if I offended anyone :mkay

Simon Lloyd
05-30-2008, 10:25 AM
ECDL really just gets the user to demonstrate they have above average computer skills i.e not just open and read e-mails but can customise settings, demonstrate word processing capabilities etc, as for Advanced level well i haven't read anything on that but i suppose it proves the candidate has a sound grasp of the inner workings or controls of certain programmes!

Oorang
05-30-2008, 01:56 PM
What I said did come out sounding really harsh.
Sorry if I offended anyone :mkay

Just hassling you a little :) :devil2:

CreganTur
06-02-2008, 05:16 AM
Just hassling you a little :) :devil2:
:slingshot