Vba code to generate a list of numbers in random order

JOAO12

New Member
Joined
Feb 1, 2013
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Searching this forum, I found the below code to generate a list of numbers in random order using vba.

I'm trying to adapt this code in order to add a new variable (x), so that the upperbound value of array V is the value of this variable (x).

VBA Code:
Sub Demo1()
    Dim x, V, L&, R&, S
        x = 132
        V = [ROW(101:x)]
    For L = UBound(V) To 2 Step -1
        R = Application.RandBetween(1, L)
        S = V(L, 1):  V(L, 1) = V(R, 1):  V(R, 1) = S
    Next
        [A1].Resize(UBound(V)).Value2 = V
End Sub

How could I rewrite this piece of code?

VBA Code:
        V = [ROW(101:x)]
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about this?

VBA Code:
V = Evaluate("TRANSPOSE(INDEX(ROW(101:" & x & "),))")
 
Upvote 1
Another option
VBA Code:
        V = Evaluate("ROW(101:" & x & ")")
 
Upvote 1
Solution
I would like to thank you both, Fluff and Irobbo314. Fluff's answer works fine.

Irobbo314's answer also works, but the use of "transpose" creates the need to change the code as shown below.

VBA Code:
Sub Demo1()
    Dim x, V, L&, R&, S
        x = 132
        V = Evaluate("TRANSPOSE(INDEX(ROW(101:" & x & "),))")
    For L = UBound(V) To 2 Step -1
        R = Application.RandBetween(1, L)
        S = V(L):  V(L) = V(R):  V(R) = S
    Next
        [A1].Resize(UBound(V)).Value2 = Application.Transpose(V)
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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