Consulting

Results 1 to 5 of 5

Thread: RAND function recalculating

  1. #1
    VBAX Regular
    Joined
    Jan 2008
    Posts
    6
    Location

    Unhappy RAND function recalculating

    I use the RAND function to generate numbers for math problems in a worksheet. The user is asked to enter an answer. However, when the answer is enetered by the user, the worksheete refreshes itself and regenerates new numbers which always makes the answer enetered is wrong. Is there anyway to make the RAND function do not regenerate new number unless it is activated by some other action.

    Please help!

    CGP

  2. #2
    RAND() is a volitile function. Someone here probably knows a more elegant way, but this seems to work.

    [vba]Function NVRand() As Double
    NVRand = VBA.Rnd
    End Function
    [/vba]
    You'll have to come up with a way to tickle it when you want to generate a new (pseudo) random number.

  3. #3
    VBAX Regular
    Joined
    Jan 2008
    Posts
    6
    Location
    Thank you for the Function! Now I need to figure out how to tickle it to generate a new random number. Does anyone has an idea?

    CGPHUNG

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]Function NVRand(inVal as Boolean) As Double
    If inVal Then
    NVRand = VBA.Rnd
    Else
    NVRand = Application.Caller.Value
    End If
    End Function [/VBA]

    Put =NVRand($A$1) in your range of cells. Enter TRUE in A1. There are the Random Values. Enter False in A1. The values are frozen, until you True again

  5. #5
    VBAX Regular
    Joined
    Jan 2008
    Posts
    6
    Location

    I got it!

    Thank you for all the sugestions! I can insert a button and assign the macro 'Refresh' to do the trick.

    [vba]
    Sub Refresh()
    Range("D7") = MyRnd()
    End Sub

    Public Function MyRnd() As Double
    MyRnd = Rnd
    End Function
    [/vba]
    CGP
    Last edited by cgphung; 01-11-2008 at 10:04 PM. Reason: Problem Solved

Posting Permissions

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