none
Random number generator RRS feed

  • Question

  • Working in Excel 10 in Windows 7.  I am using the following code to generate a random number between two numbers entered into a user form.  The result, "WinNum" is always 0 (zero).  What's wrong with my code?  Is there an Add-In  in Excel that I'm missing?  Thanks in advance

    Doug

    Private Sub cmdPickWinner_Click()

    ' Generate a Random Number

        Dim StartNum, EndNum, WinNum As Integer
       
        StartNum = txtFirstNumber.Value
        EndNum = txtLastNumber.Value
        txtWinningNumber.Value = WinNum
       
        Randomize
        WinNum = CInt((EndNum - StartNum + 1) * Rnd()) + StartNum
     
    End Sub

    Saturday, October 3, 2015 8:46 PM

Answers

  • Move the txtWinningNumber.Value = WinNum so that it is after the Randomize

    Dim StartNum, EndNum, WinNum As Integer
    StartNum = InputBox("Enter the first Number")
    EndNum = InputBox("Enter the last Number")
    Randomize
    WinNum = CInt((EndNum - StartNum + 1) * Rnd()) + StartNum
    MsgBox WinNum


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org

    Sunday, October 4, 2015 4:00 AM
    Moderator