Macro to randomly choose numbers

GeeBDee

New Member
Joined
Dec 7, 2015
Messages
36
Hi

I would like to click on a button to have a macro run and choose any amount of random numbers between 1 - 10. So click the button and it may choose 3 random numbers between 1 - 10, click it again and it may choose 5 random numbers, then click and it chooses 9 random numbers and so on.

I would like to know if there is anyway a macro is capable of this and if so, please advise the code?

Or am I asking too much?

If this is not possible then a macro to just choose 1 random number between 1 - 10 please.

Grant
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
without a macro i guess this works :


Excel 2010
ABCDEFGHIJKL
1
2from1
3to10
4choose5
5
6
78627000000
Sheet1
Cell Formulas
RangeFormula
C7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(C6)-2)<$D$4)
D7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(D6)-2)<$D$4)
E7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(E6)-2)<$D$4)
F7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(F6)-2)<$D$4)
G7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(G6)-2)<$D$4)
H7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(H6)-2)<$D$4)
I7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(I6)-2)<$D$4)
J7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(J6)-2)<$D$4)
K7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(K6)-2)<$D$4)
L7=RANDBETWEEN($D$2,$D$3)*(--(COLUMN(L6)-2)<$D$4)


the -2 thing after the COLUMN function is just to count for columns A and B;

i formatted the cells where the numbers appears using the following custom format:

Code:
[White][=0]General;[Black]General
 
Upvote 0
another take with macros on the same sheet structure i presented above:

Code:
Sub randomgen()

Range(Range("c7"), Range("c7").End(xlToRight)).Clear


Dim F As Integer: F = Range("d2").Value
Dim T As Integer: T = Range("d3").Value
Dim R As Integer: R = Range("d4").Value




For Each cell In Range(Cells(7, 3), Cells(7, R + 2))
cell.Value = Application.WorksheetFunction.RandBetween(F, T)
Next cell




End Sub

link the macro with a shape
 
Last edited:
Upvote 0
Thank you Speedsheetcrusader, that works very well. But I really would like to have it via a macro so that a button has to be clicked.

Grant
 
Upvote 0
bro there was some error in my previous code :p

i fixed it

check it again please

Code:
Sub randomgen()

Range(Range("c7"), Range("c7").End(xlToRight)).Clear


Dim F As Integer: F = Range("d2").Value
Dim T As Integer: T = Range("d3").Value
Dim R As Integer: R = Range("d4").Value




For Each cell In Range(Cells(7, 3), Cells(7, R + 2))
cell.Value = Application.WorksheetFunction.RandBetween(F, T)
Next cell




End Sub
 
Upvote 0
Yes that macro works perfectly, thank you so much.

I do have another question?

Can I associate a picture to a number that is radomly chosen? This is for display purposes.
 
Upvote 0
that is interesting.

but i am still a noob that is beyond my skills

i guess you will have to wait for the geeks, scientists, nerds , Excel gods to visit your thread and shows us how this can be done :biggrin: i am sure it is do-able, but that is beyond me.:(
 
Upvote 0
Ha ha thank you for what you have advised anyway, you have been very helpful. Maybe I will start a new thread to ask this question.

Cheers
G
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

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