Results 1 to 4 of 4

Thread: Generate random numbers in Excel

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0

    Generate random numbers in Excel

    Hi

    i am looking to generate random numbers in excel.
    Step :
    1. Select the Range
    2. Input box will ask From Number; To Number
    3. when i click on ok it will generate random numbers within selected range if possible they should be unique

    reference : http://www.ablebits.com/excel-random...dins/index.php
    http://www.ozgrid.com/VBA/RandomNumbers.htm

    i will appreciate any help on this

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Code:
    Sub GenerateRandomNumbers()
    
        Dim lngFrom As Long, lngTo As Long
        Dim rng As Range
        
        lngFrom = InputBox("Enter from value")
        lngTo = InputBox("Enter to value. Should be greater than " & lngFrom)
        
        For Each rng In Selection 'Will error out if selection is not a range
            rng.Value = GETRANDBETWEEN(lngFrom, lngTo)
        Next rng
        
    End Sub
    Function GETRANDBETWEEN(lngFrom As Long, lngTo As Long)
    
        GETRANDBETWEEN = lngFrom + Int(Rnd() * (lngTo - lngFrom + 1))
        
    End Function
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    You could always use =RANDBETWEEN() in Excel 2007 and greater, in which case you could modify the function to
    Code:
    Function GETRANDBETWEEN(lngFrom As Long, lngTo As Long)
    
        GETRANDBETWEEN = Application.RandBetween(lngFrom, lngTo)
        
    End Function
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Rep Power
    0
    Thank u so much Kris !
    Really appreciate your knowledge & help

Similar Threads

  1. Get Random List :
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 06-06-2013, 07:53 PM
  2. VBA Code to Generate Events at Specific frequency
    By ChiswickBridge in forum Excel Help
    Replies: 0
    Last Post: 02-11-2013, 08:20 AM
  3. Replies: 2
    Last Post: 12-04-2012, 06:09 PM
  4. Replies: 1
    Last Post: 12-04-2012, 05:30 PM
  5. Random Unique Number Generator Excel VBA
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 5
    Last Post: 10-18-2012, 01:00 PM

Tags for this Thread

Posting Permissions

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