+ Reply to Thread
Results 1 to 3 of 3

RandBetween code in VB (Excel 2010)

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2012
    Location
    michigan
    MS-Off Ver
    Excel 2010
    Posts
    2

    RandBetween code in VB (Excel 2010)

    I have to create a code in excel that requires the user to select a number between 1 and 100 and tell the user to guess the value of the randomly generated number and to let the user know if it is correct, too low, or too high.

    The game requires a button to be created in a procedure labeled "play the high low game"

    I know I need to use WorksheetFunction.RandBetween(1,100) but I don't know what else needs to be done.

    Please help.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    You don't need to use RandBetween, you can use VBA's Rnd.
    randno = Int(Rnd()*100) + 1
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: RandBetween code in VB (Excel 2010)

    This should do the trick:

    Sub test()
      Dim Response%, Ans%
        On Error Resume Next
        Ans = Application.RandBetween(1, 100)
    TryAgain:
        Response = CInt(InputBox("Pick a number between 1 and 100"))
        If Response > 0 Or Response < 100 Then
            If Ans = Response Then
                MsgBox "Correct Answer", vbExclamation
                Ans = 0
            ElseIf Ans < Response Then
                If MsgBox("Try a lower number", vbExclamation + vbOKCancel) = vbOK Then GoTo TryAgain
            ElseIf Ans > Response Then
                If MsgBox("Try a higher number", vbExclamation + vbOKCancel) = vbOK Then GoTo TryAgain
            End If
        End If
    End Sub
    You can extend this to using a userform instead of an input box, etc.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1