VBA : Allocate Data Randomly and Evenly

Samantha27

New Member
Joined
Jan 30, 2024
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hello Good People,

I have this allocation workbook that when I click a button. It will randomly divide the list of data into 8. (Currently 8 but it will soon increase).

I have attached a sample images for my 2 sheets.

Persons sheet - List of data
Teams sheet - Output of allocation

However, I'm having a problem with this line of code

VBA Code:
teamsSheet.Cells(startRow, startCol).Value = personsSheet.Cells(personNumber, 2).Value

Also, it only set to 10 and should exclude the header of Persons sheet whenever I click the button.

What I wanted to achieve is to divide the data randomly and evenly to the 8 employee.

Below is my full code. Hoping someone could help me on this, Thank youuuu guys in advance.

VBA Code:
Public Sub CreateTeams_BtnClick()
CreateTeams
End Sub
Private Sub CreateTeams()
Dim teamsSheet, personsSheet As Worksheet
Set teamsSheet = Worksheets("Teams")
Set personsSheet = Worksheets("Persons")


personsSheet.Range("A:A").Copy Destination:=personsSheet.Range("B:B")

Dim numPersons As Integer
numPersons = personsSheet.Range("B:B").End(xlDown).Row

Dim startRow As Integer
startRow = 2
Dim startCol As Integer
startCol = 1
Dim personNumber As Integer

For i = 2 To 8
    For j = 1 To 10
        personNumber = Int((numPersons - 1 + 1) * Rnd() + 1)
        teamsSheet.Cells(startRow, startCol).Value = personsSheet.Cells(personNumber, 2).Value
        personsSheet.Cells(personNumber, 2).Delete Shift:=xlUp
        numPersons = numPersons - 1
        startRow = startRow + 1
    Next j

    If i < 8 Then
        startRow = 2
        startCol = startCol + 1
    ElseIf i = 8 Then
        startRow = 14
        startCol = 1
    Else
        startRow = 14
        startCol = startCol + 1
    End If
Next i
End Sub
 

Attachments

  • Persons.jpg
    Persons.jpg
    59.8 KB · Views: 13
  • Teams.jpg
    Teams.jpg
    105.7 KB · Views: 13

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
See if this works for you:

Rich (BB code):
Public Sub CreateTeams_BtnClick()
    CreateTeams
End Sub

Private Sub CreateTeams()
Dim TopPerson As Range, TopTeam As Range, MyOut() As Variant, Persons As Variant
Dim NumTeams As Long, NP As Long, r As Long, c As Long, x As Double

    Set TopPerson = Worksheets("Persons").Range("A2")
    Set TopTeam = Worksheets("Teams").Range("A2")
    NumTeams = 8

    Persons = Range(TopPerson, TopPerson.End(xlDown)).Value
    NP = UBound(Persons)
    ReDim MyOut(1 To Int(NP / NumTeams) + IIf((NP Mod NumTeams) > 0, 1, 0), 1 To NumTeams)
   
    r = 1
    c = 1
    While NP > 0
        x = Int(Rnd() * NP) + 1
        MyOut(r, c) = Persons(x, 1)
        Persons(x, 1) = Persons(NP, 1)
        c = c + 1
        If c > NumTeams Then
            c = 1
            r = r + 1
        End If
        NP = NP - 1
    Wend
   
    TopTeam.Resize(UBound(MyOut), NumTeams).Value = MyOut
       
End Sub

In the red lines, set the first line to the sheet name and top cell of the person list. Set the second line to the sheet name and top left cell of the team list. Set the third line to how many teams you want. Let us know how this works.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

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