PDA

View Full Version : Generate random numbers in Excel



Mahesh
10-06-2011, 01:04 AM
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-generator-assistant-free-addins/index.php
http://www.ozgrid.com/VBA/RandomNumbers.htm

i will appreciate any help on this

Excel Fox
10-06-2011, 02:13 AM
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

Excel Fox
10-06-2011, 02:21 AM
You could always use =RANDBETWEEN() in Excel 2007 and greater, in which case you could modify the function to
Function GETRANDBETWEEN(lngFrom As Long, lngTo As Long)

GETRANDBETWEEN = Application.RandBetween(lngFrom, lngTo)

End Function

Mahesh
10-06-2011, 11:24 AM
Thank u so much Kris ! :)
Really appreciate your knowledge & help :cheerio: