Consulting

Results 1 to 19 of 19

Thread: Macro to generate random number then User needs to guess that value

  1. #1
    VBAX Newbie
    Joined
    May 2008
    Posts
    3
    Location

    Macro to generate random number then User needs to guess that value

    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.

    [VBA]
    '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
    [/VBA]

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    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?
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  4. #4
    VBAX Newbie
    Joined
    May 2008
    Posts
    3
    Location
    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.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Having Excel might be useful to test it.

    How are you testing it without Excel?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    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:
    [VBA]
    UserInput= Application.InputBox(Message,"Number Guessing",,,,,,1)
    [/VBA]where 1 is the type number for numbers only!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Quote Originally Posted by xld
    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?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Works ok for me too. If you think it's not, what condition does it fail under?
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  9. #9
    VBAX Newbie
    Joined
    May 2008
    Posts
    3
    Location
    Quote Originally Posted by xld
    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.

    Quote Originally Posted by Oorang
    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.

  10. #10
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    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

  11. #11
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Not to be rude... but why are you not developing and testing in Excel if this is for an advanced Excel class?
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by CreganTur
    Not to be rude... but why are you not developing and testing in Excel if this is for an advanced Excel class?
    And if this is advanced, what are the intermediate and basic classes like?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    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


    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Quote Originally Posted by mdmackillop


    From an Advanced ECDL course
    If that's an advanced class, then can I go ahead and get my diploma now
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  15. #15
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location



    Quote Originally Posted by CreganTur
    If that's an advanced class, then can I go ahead and get my diploma now
    Yes yes, we get it, "CreganTur smart, whole world dumb." we get it. Here's your cookie , off you go now ...

    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  16. #16
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Quote Originally Posted by Oorang
    Yes yes, we get it, "CreganTur smart, whole world dumb." we get it. Here's your cookie , off you go now ...
    What I said did come out sounding really harsh.

    Sorry if I offended anyone
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  17. #17
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    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!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  18. #18
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Quote Originally Posted by CreganTur
    What I said did come out sounding really harsh.
    Sorry if I offended anyone
    Just hassling you a little
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  19. #19
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Quote Originally Posted by Oorang
    Just hassling you a little
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •