Fill random 3 columns.

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010

Hello, I want to be filled random 1’s, X’s & 2’s as per selected amount in the columns C, D & E…for example column C in the range C6:C19 there are 14 cells…

Step-1 fills 5 times 1’s as per specified quantity 5 in C5…in the range C6:C19
Step-2 fills 5 times X’s as per specified quantity 5 in D5… in the range D6:D19 But within empty row which are not filled by 1’s…
Step-3 fills 4 times 2’s as per specified quantity 4 in E5… in the range E6:E19 But within empty row which are not filled by 1’s & X’s…

Note: Number for the could be vary in the 1’s, X’s & 2’s could be vary in the range C5:E5…Please suggest VBA solution

Shuffle.xlsm
ABCDEF
1
2
3RandomRandomRandom
41X2
5554
611
72X
832
941
105X
1162
127X
1381
149X
15102
16111
1712X
18131
19142
20
21
22
23
24
25
26
Sheet1


Regards,
Moti
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this:

VBA Code:
Public Sub Random3()
Dim i&, k&, sum3&
Dim sArr, rArr
sArr = Range("C4:E5").Value
sum3c = sArr(2, 1) + sArr(2, 2) + sArr(2, 3)
ReDim rArr(1 To sum3c, 1 To 3)

For i = 1 To sum3c
    Do
        k = Application.RandBetween(1, 3)
    Loop While sArr(2, k) = 0
        sArr(2, k) = sArr(2, k) - 1
        rArr(i, k) = sArr(1, k)
Next i
Range("C6").Resize(sum3c, 3).Value = rArr
End Sub
 
Upvote 1
Solution
Phuoc, thank you very much this is a perfect solution what I wanted. :)

One additional request if it is viable for example if I Pre-select number of 1’s, x’s & 2 in the column C in the range C6:C19 does it is possible to shuffle them in the same column or get shuffle result in the column D as shown. Please could you take a look?

Shuffle.xlsm
ABCDE
1
2
3
4Pre SelectedShaffle
51X2HERE
611X
7211
8312
941X
10511
11612
127X1
138XX
149X2
151022
161122
171221
181321
191421
20
21
22
Sheet2


Have a blessed week ahead.

Kind Regards,
Moti
 
Upvote 0
Phuoc, thank you very much this is a perfect solution what I wanted. :)

One additional request if it is viable for example if I Pre-select number of 1’s, x’s & 2 in the column C in the range C6:C19 does it is possible to shuffle them in the same column or get shuffle result in the column D as shown. Please could you take a look?

Shuffle.xlsm
ABCDE
1
2
3
4Pre SelectedShaffle
51X2HERE
611X
7211
8312
941X
10511
11612
127X1
138XX
149X2
151022
161122
171221
181321
191421
20
21
22
Sheet2


Have a blessed week ahead.

Kind Regards,
Moti
Hello, I found “VBA Shuffle Cells… in MrExcel” may it can be useful for someone. Have a nice day.Good Luck

Kind Regards,
Moti

VBA Code:
'https://www.mrexcel.com/board/threads/shuffle-cells.367226/#post-1820408
'Shuffle Cells

Sub Shuffle_Columns_Cells() 'By jindon
Dim a(), i As Long
a = Range("C6", Range("C" & Rows.Count).End(xlUp)).Resize(, 2).Value
Randomize
For i = 1 To UBound(a, 1)
    a(i, 2) = Rnd
Next
VSortMA a, 1, UBound(a, 1), 2
Range("D6").Resize(UBound(a, 1)).Value = a
End Sub
 
Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, i As Long, ii As Long, iii As Long
i = UB: ii = LB
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
     Do While ary(ii, ref) < M
          ii = ii + 1
     Loop
     Do While ary(i, ref) > M
          i = i - 1
     Loop
     If ii <= i Then
          For iii = LBound(ary, 2) To UBound(ary, 2)
               temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
          Next
          ii = ii + 1: i = i - 1
     End If
Loop
If LB < i Then VSortMA ary, LB, i, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub
 
Upvote 0
Or use this code:

VBA Code:
Sub Random2()

Dim arr, i As Long, u As Long, k As Long, temp As String
arr = Range("a1", Range("a" & Rows.Count).End(xlUp)).Value
Randomize
u = UBound(arr, 1)
For i = 1 To u
    k = Int(Rnd() * u) + 1
    temp = arr(i, 1)
    arr(i, 1) = arr(k, 1)
    arr(k, 1) = temp
Next

Range("B2").Resize(u).Value = arr

End Sub
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,173
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