Thread: VBA Randomly Distribute A,B,C,D,E Evenly Thanks: 0 Likes:  1 Post #5327393 (1)

1. VBA Randomly Distribute A,B,C,D,E Evenly

I have a data set that I would like to have A,B,C,D,E distributed evenly. Below is a sample, I would like to use this on a larger data set. If anyone has a Function or VBA I would appreciate it. Thanks in advance!

STUDENT 1 B
STUDENT 2 C
STUDENT 3 D
STUDENT 4 E
STUDENT 5 B
STUDENT 6 A
STUDENT 7 D
STUDENT 8 A
STUDENT 9 C
STUDENT 10 A
STUDENT 11 B
STUDENT 12 E
STUDENT 13 D
STUDENT 14 C
STUDENT 15 C
STUDENT 16 A
STUDENT 17 E
STUDENT 18 B
STUDENT 19 E
STUDENT 20 D

2. Re: VBA Randomly Distribute A,B,C,D,E Evenly

 A B C D E F 1 Group Qty CDF Check 2 A 5 0 5 C2: =SUM(B\$1:B1) 3 B 5 5 5 D2: =COUNTIF(\$B\$10:\$B\$29, A2) 4 C 5 10 5 5 D 5 15 5 6 20 B6: =SUM(B2:B5) 7 8 9 Student Group 10 1 B B10: {=INDEX(A\$2:A\$5, MATCH(RANDBETWEEN(0, B\$6 - ROWS(B\$9:B9)), \$C\$2:\$C\$5 - COUNTIF(B9:B\$9, "<" & A\$2:A\$5)))} 11 2 C 12 3 D 13 4 A 14 5 B 15 6 D 16 7 A 17 8 A 18 9 C 19 10 B 20 11 A 21 12 D 22 13 D 23 14 A 24 15 B 25 16 C 26 17 D 27 18 C 28 19 B 29 20 C

3. Re: VBA Randomly Distribute A,B,C,D,E Evenly

Assuming your data is located in Column A and starts on Row 1 (change the red highlighted text if that guess is wrong), give this macro a try...
Code:
```Sub RandomEvenDistributionOf()
Dim R As Long, Cnt As Long, RndIdx As Long, HowMany As Long, What As String
Dim Tmp As Variant, Data As Variant, Arr As Variant
Data = Range("A1", Cells(Rows.Count, "A").End(xlUp))
What = "ABCDE"
HowMany = UBound(Data)
Arr = Split(Trim(Replace(StrConv(Application.Rept(What, 1 + Int(HowMany / Len(What))), vbUnicode), Chr(0), " ")))
Randomize
For Cnt = UBound(Arr) To LBound(Arr) Step -1
RndIdx = Int((Cnt - LBound(Arr) + 1) * Rnd + LBound(Arr))
Tmp = Arr(RndIdx)
Arr(RndIdx) = Arr(Cnt)
Arr(Cnt) = Tmp
Next
Range("A1").Offset(,1).Resize(HowMany) = Application.Transpose(Arr)
End Sub```
Note: If the number of names is not an even multiple of the number of letters you want to distribute, the "odd excess" will be filled randomly from among the letters being distributed.

4. Re: VBA Randomly Distribute A,B,C,D,E Evenly

another VBA option
- if there are 20 students all grades occur 4 times
- if there are 21 students 4 grades occur 4 times, 1 grade occurs 5 times
- if there are 20 students 3 grades occur 4 times, 2 grades occur 5 times
- if there are 20 students 2 grades occur 4 times, 3 grades occur 5 times
- if there are 20 students 1 grade occurs 4 times, 4 grades occur 5 times
- if there are 25 students all grades occur 5 times etc

Code:
```Sub Distribute()
Dim Ws As Worksheet, Rng As Range, Cel As Range, M As Integer, C As Long, a As Long, xMax As Long
Set Ws = ActiveSheet
Set Rng = Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp)).Offset(, 1)
Rng.ClearContents
C = Rng.Cells.CountLarge            'number of students
M = C Mod 5                         'remainder when C is divided by 5
xMax = (C - M) / 5                  'max occurrence for even distribution
'allocate excluding remainder
For a = 1 To C - M
Set Cel = Rng(a, 1)
Cel = Chr(WorksheetFunction.RoundUp(Rnd * 5, 0) + 64)
If WorksheetFunction.CountIf(Rng, Cel) > xMax Then a = a - 1
Next a
'allocate the remainder
For a = C - M + 1 To C
Set Cel = Rng(a, 1)
If WorksheetFunction.CountIf(Rng, Cel) > xMax + 1 Then a = a - 1
Next a```
End Sub

Student names are in column A starting at A2 (without any blank cells)

Results will differ every time the code is run
- example

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
1
2
STUDENT 1 B A
4
3
STUDENT 2 E B
5
4
STUDENT 3 D C
5
5
STUDENT 4 B D
4
6
STUDENT 5 B E
5
7
STUDENT 6 E
8
STUDENT 7 C
9
STUDENT 8 E
10
STUDENT 9 C
11
STUDENT 10 B
12
STUDENT 11 E
13
STUDENT 12 A
14
STUDENT 13 A
15
STUDENT 14 D
16
STUDENT 15 D
17
STUDENT 16 C
18
STUDENT 17 D
19
STUDENT 18 C
20
STUDENT 19 A
21
STUDENT 20 A
22
STUDENT 21 C
23
STUDENT 22 E
24
STUDENT 23 B
25
 Sheet: Sheet1

5. Re: VBA Randomly Distribute A,B,C,D,E Evenly

With formulas

 A B C D E 1 STUDENT FINAL GROUP RANDOM GROUP repeat from A to B CHECK 2 STUDENT 1 A 0.950053729 A 4 3 STUDENT 2 B 0.162449816 B 4 4 STUDENT 3 E 0.493879798 C 4 5 STUDENT 4 C 0.36720584 D 4 6 STUDENT 5 D 0.301664141 E 4 7 STUDENT 6 D 0.558129856 A 8 STUDENT 7 C 0.274059549 B 9 STUDENT 8 B 0.415170288 C 10 STUDENT 9 E 0.182926025 D 11 STUDENT 10 E 0.092627143 E 12 STUDENT 11 C 0.161204425 A 13 STUDENT 12 A 0.476992103 B 14 STUDENT 13 A 0.278864691 C 15 STUDENT 14 C 0.57026593 D 16 STUDENT 15 B 0.275809365 E 17 STUDENT 16 A 0.817885174 A 18 STUDENT 17 B 0.690742645 B 19 STUDENT 18 D 0.104926195 C 20 STUDENT 19 E 0.28160362 D 21 STUDENT 20 D 0.273532275 E

 Cell Formula B2 =INDEX(\$D\$2:\$D\$21,RANK(C2,\$C\$2:\$C\$21)) C2 =RAND() E2 =COUNTIF(\$B\$2:\$B\$21,D2)

After obtaining the results, you must copy column C and paste as values ​​so that the results no longer move.

6. Re: VBA Randomly Distribute A,B,C,D,E Evenly

Originally Posted by Yongle
Code:
```Sub Distribute()
Dim Ws As Worksheet, Rng As Range, Cel As Range, M As Integer, C As Long, a As Long, xMax As Long
Set Ws = ActiveSheet
Set Rng = Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp)).Offset(, 1)
Rng.ClearContents
C = Rng.Cells.CountLarge            'number of students
M = C Mod 5                         'remainder when C is divided by 5
xMax = (C - M) / 5                  'max occurrence for even distribution
'allocate excluding remainder
For a = 1 To C - M
Set Cel = Rng(a, 1)
Cel = Chr(WorksheetFunction.RoundUp(Rnd * 5, 0) + 64)
If WorksheetFunction.CountIf(Rng, Cel) > xMax Then a = a - 1
Next a
'allocate the remainder
For a = C - M + 1 To C
Set Cel = Rng(a, 1)
If WorksheetFunction.CountIf(Rng, Cel) > xMax + 1 Then a = a - 1
Next a```
End Sub
What is GetGrade (see red highlighted text above)? This seems to be the only appearance of it in all of your posted code.

Also, you need to include a Randomize statement in your code or I think it will repeat the same sequence each time the workbook is opened.

7. Re: VBA Randomly Distribute A,B,C,D,E Evenly

Thanks @Rick Rothstein

ooops
I had a function which I decided to remove - changed one line and forgot to change the other !!
Of couse it still worked for me

Code:
```Sub Distribute()
Dim Ws As Worksheet, Rng As Range, Cel As Range, M As Integer, C As Long, a As Long, xMax As Long
Set Ws = ActiveSheet
Set Rng = Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp)).Offset(, 1)
Rng.ClearContents
C = Rng.Cells.CountLarge            'number of students
M = C Mod 5                         'remainder when C is divided by 5
xMax = (C - M) / 5                  'max occurrence for even distribution
'allocate excluding remainder
For a = 1 To C - M
Set Cel = Rng(a, 1)
Cel = Chr(WorksheetFunction.RoundUp(Rnd * 5, 0) + 64)
If WorksheetFunction.CountIf(Rng, Cel) > xMax Then a = a - 1
Next a
'allocate the remainder
For a = C - M + 1 To C
Set Cel = Rng(a, 1)
Cel = Chr(WorksheetFunction.RoundUp(Rnd * 5, 0) + 64)
If WorksheetFunction.CountIf(Rng, Cel) > xMax + 1 Then a = a - 1
Next a
End Sub```

8. Re: VBA Randomly Distribute A,B,C,D,E Evenly

Also, you need to include a Randomize statement in your code or I think it will repeat the same sequence each time the workbook is opened
@Rick Rothstein - tested that and you are correct

9. Re: VBA Randomly Distribute A,B,C,D,E Evenly

Wow!!!! Thank you all!! I greatly appreciate it!!!! I appreciate all of the approaches!!!