I need help for a Random Number Generator

xr600rr

New Member
Joined
May 20, 2003
Messages
8
Hello, I need some help. What I need is a random number generator for numbers 1-9. It needs: 1 number per cell in a column and the numbers can not be the same. i.e.:
4
1
2
6
8
5
3
7
9

I have a VB mod that will produce the numbers but all the numbers appear in the same cell:

Function RandLotto(Bottom As Integer, Top As Integer, _
Amount As Integer) As String
Dim iArr As Variant
Dim i As Integer
Dim r As Integer
Dim temp As Integer

Application.Volatile

ReDim iArr(Bottom To Top)
For i = Bottom To Top
iArr(i) = i
Next i
For i = Top To Bottom + 1 Step -1
r = Int(Rnd() * (i - Bottom + 1)) + Bottom
temp = iArr(r)
iArr(r) = iArr(i)
iArr(i) = temp
Next i
For i = Bottom To Bottom + Amount - 1
RandLotto = RandLotto & " " & iArr(i)
Next i
RandLotto = Trim(RandLotto)
End Function

With =randlotto(1,9,1) as the formula for the cell.

Any Ideas?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Why don't you use your VBA Function in a cell, then use formulae to extract the numbers into separate cells.

For example, if the formula =RandLotto(1,9,9) is in cell A1, then use this in cell A2 :
=MID(A1,1,1)
in A3 =MID(A1,3,1)
in A4 =MID(A1,5,1)
etc etc


Glenn.
 
Upvote 0
Well, Theres a new problem now. My vlookup does not work when the cell with the =mid ref in it.

=VLOOKUP(1,B14:D22,3,FALSE)

b14 is where the =MID(B39,3,1) is and now on the vlookup cell is #N/A???
 
Upvote 0
Ah, you didn't say that you wanted to use them as numbers. Simply change the formula to be =VALUE(MID(B39,3,1)) and it will work.


Glenn.
 
Upvote 0
Did you do the VALUE() stuff for all three formulas?

For other ways of solving the problem, including non-programmatic solutions, check the Excel | Tutorials | Random Selection page of my web site.
 
Upvote 0
I'm no VLOOKUP expert, and forgive me for butting in, but I'd guess that your lookup table hasn't been sorted, would something like this work?

=VLOOKUP(1,B14:D22,3,TRUE)
 
Upvote 0
Mark O'Brien said:
I'm no VLOOKUP expert, and forgive me for butting in, but I'd guess that your lookup table hasn't been sorted, would something like this work?

=VLOOKUP(1,B14:D22,3,TRUE)

Mark, TRUE is the range_lookup value that requires the table_array to be sorted on its leftmost column -- not FALSE.
 
Upvote 0

Forum statistics

Threads
1,221,701
Messages
6,161,381
Members
451,700
Latest member
Eccymarge

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top