VBA Generate random 10 Group of 5 numbers

motilulla

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

Looking VBA solution which generate random 10 group of 5 numbers using all 50 numbers in cells D2:H11
List of numbers is in cells A2:A51.
If all numbers are used in to cross check C1 will show sum of 50 numbers = 1275
Example…


Book1
ABCDEFGH
1Numbers1275n1n2n3n4n5
21Group1344192023
32Group221155057
43Group3191118308
54Group43213481231
65Group5143463938
76Group63649162633
87Group71044352737
98Group8251734524
109Group9404221429
1110Group1042822476
1211
1312
1413
1514
1615
1716
1817
1918
2019
2120
2221
2322
2423
2524
2625
2726
2827
2928
3029
3130
3231
3332
3433
3534
3635
3736
3837
3938
4039
4140
4241
4342
4443
4544
4645
4746
4847
4948
5049
5150
Random-50
Cell Formulas
RangeFormula
C1=SUM(D2:H11)


Thanks In Advance
Using version 2000

Regards,
Moti
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Jun47
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, nRay() [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] nRdn [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
Ac = 1
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
 Ray = Application.Transpose(Rng)
  ReDim nRay(1 To 10, 1 To 5)
                Randomize
    [COLOR="Navy"]Do[/COLOR] Until c * Ac = 50
        nRdn = Int(Rnd * Rng.Count) + 1
        [COLOR="Navy"]If[/COLOR] Not Ray(nRdn) = vbNullString [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] c = 10 [COLOR="Navy"]Then[/COLOR]
                c = 0: Ac = Ac + 1
            [COLOR="Navy"]End[/COLOR] If
            c = c + 1
            nRay(c, Ac) = Ray(nRdn)
            Ray(nRdn) = vbNullString
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Loop[/COLOR]
Range("D2").Resize(10, 5) = nRay
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG26Jun47
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] Ray [COLOR=navy]As[/COLOR] Variant, nRay() [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Integer,[/COLOR] nRdn [COLOR=navy]As[/COLOR] [COLOR=navy]Integer,[/COLOR] Ac [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
Ac = 1
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
 Ray = Application.Transpose(Rng)
  ReDim nRay(1 To 10, 1 To 5)
                Randomize
    [COLOR=navy]Do[/COLOR] Until c * Ac = 50
        nRdn = Int(Rnd * Rng.Count) + 1
        [COLOR=navy]If[/COLOR] Not Ray(nRdn) = vbNullString [COLOR=navy]Then[/COLOR]
            [COLOR=navy]If[/COLOR] c = 10 [COLOR=navy]Then[/COLOR]
                c = 0: Ac = Ac + 1
            [COLOR=navy]End[/COLOR] If
            c = c + 1
            nRay(c, Ac) = Ray(nRdn)
            Ray(nRdn) = vbNullString
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Loop[/COLOR]
Range("D2").Resize(10, 5) = nRay
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
MickG, VBA results as appeal. Thank You!!

May I ask you if it is not much trouble for you, Please can you make it arrange the rows in ascending order?

Regards,
Moti
 
Upvote 0
How would that look ???
Perhaps sort each column or each row ?????
Example please !!
Mick, Results rows in ascending order

For example this…


Book1
CDEFGH
11275n1n2n3n4n5
2Group1642471840
3Group2282144515
4Group31039451230
5Group44321183
6Group511926249
7Group63525162429
8Group7312774822
9Group83338373443
10Group95041461720
11Group10361323149
Random-50


To this…


Book1
CDEFGH
11275n1n2n3n4n5
2Group1618404247
3Group2515212844
4Group31012303945
5Group43481132
6Group512192649
7Group61624252935
8Group7722273148
9Group83334373843
10Group91720414650
11Group10913142336
Random-50-1


Thank you

Regards,
Moti
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Jun42
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, nRay() [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] nRdn [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
Ac = 1
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
 Ray = Application.Transpose(Rng)
  ReDim nRay(1 To 10, 1 To 5)
                Randomize
    [COLOR="Navy"]Do[/COLOR] Until c * Ac = 50
        nRdn = Int(Rnd * Rng.Count) + 1
        [COLOR="Navy"]If[/COLOR] Not Ray(nRdn) = vbNullString [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] c = 10 [COLOR="Navy"]Then[/COLOR]
                c = 0: Ac = Ac + 1
            [COLOR="Navy"]End[/COLOR] If
            c = c + 1
            nRay(c, Ac) = Ray(nRdn)
            Ray(nRdn) = vbNullString
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Loop[/COLOR]
Range("D2").Resize(10, 5) = nRay
[COLOR="Navy"]Set[/COLOR] Rng = Range("D2:D11")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Dn.Resize(, 5), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    [COLOR="Navy"]With[/COLOR] ActiveSheet.Sort
        .SetRange Dn.Resize(, 5)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]Next[/COLOR] Dn

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Regards Mick
Hello MickG,

I tried lot to get fix highlighting following line.
Code:
 ActiveSheet.Sort.SortFields.Clear
No luck, need your help again please can you fix it?

Thank you

Regards,
Moti
 
Upvote 0
I'm not sure what your problem is !!
Compare your problem with this example of Your sheet.
Mick, I realized it is my older version problem, which limits many functions this does not work, needs to change latest one....

Post #3 is code working like a charm!! It is perfect

I appreciate you time and help!! :)

Regards,
Moti
 
Upvote 0
Try this with different sort code.
Code:
[COLOR="Navy"]Sub[/COLOR] MG26Jun00
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, R [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, nRay() [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] nRdn [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
Ac = 1
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
 Ray = Application.Transpose(Rng)
  ReDim nRay(1 To 10, 1 To 5)
                Randomize
    [COLOR="Navy"]Do[/COLOR] Until c * Ac = 50
        nRdn = Int(Rnd * Rng.Count) + 1
        [COLOR="Navy"]If[/COLOR] Not Ray(nRdn) = vbNullString [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] c = 10 [COLOR="Navy"]Then[/COLOR]
                c = 0: Ac = Ac + 1
            [COLOR="Navy"]End[/COLOR] If
            c = c + 1
            nRay(c, Ac) = Ray(nRdn)
            Ray(nRdn) = vbNullString
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Loop[/COLOR]
Range("D2").Resize(10, 5) = nRay
[COLOR="Navy"]Set[/COLOR] Rng = Range("D2:D11")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
  R = Dn.Resize(, 5).Value
    [COLOR="Navy"]For[/COLOR] Ac = 1 To 5
        Dn(, Ac) = Application.Small(R, Ac)
    [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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