adding loop in VBA

Jan Kalop

Active Member
Joined
Aug 3, 2012
Messages
389
I have already VBA for random numbers, but I would like to add loop for how many times the random is run and the number foe sample is in Z25

"Sub TwentyfiveRandOf25()
Dim anArray(1 To 25) As Long
Dim i As Long
Dim randIndex As Long, temp As Long
For i = 1 To 25
anArray(i) = i
Next i
For i = 1 To 25
Randomize
Do
randIndex = (Rnd() * 25) + 1
Loop Until randIndex <= 25
temp = anArray(randIndex)
anArray(randIndex) = anArray(i)
anArray(i) = temp
Next i
Range("Aa1:aa25").Value = Application.Transpose(anArray)
End Sub"
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Maybe I add a bit more information:
That VBA function produce 25 numbers (without repeats ) in column AA1:AA25. Every time I click on the button to run that VBA the numbers are generated in unique way from 1 to 25 but not in ascending or descending order but randomly, but only once.
What I would like to happens is that every time I click on the button to run that VBA it is run as many time as is the number in cell Z25 and for sample it can be any number from 1 to 30, which mean that the random numbers would have to be run and generate Z25 times that unique set of numbers.
Is that possible to add any additional scrip to that existing VBA to make it happen?
Thanks everyone for help.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,104
Members
453,021
Latest member
Justyna P

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