Assign Name to a group (VBA)

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,377
I have a list of people name in column A. starting in cell A1 ,There is anywhere from 10 to 100 names on the list.

I need to assign these people to a class room number (Room A, B, C .....) there may be up to 9 rooms available on any given day.

I am looking for VBA code to assign the people into a room. by putting the room number in column B

I will use:

people= Cells(Rows.Count, "A").End(xlUp).Row 'this will tell me how many people are registered
rooms= inputbox ("How many rooms are available today") 'number of groups to break the list into

I know i could go Down the list and write A,B,C,D then A,B,C,D then A,B,C,D etc... but that is not what i want.
i want the list to go A,A,A,A B,B,B,B, C,C,C,C D,D,D,D, .....

Examples.

43 people and 5 rooms - Room A would have the first 9 people on the list, Room B the next 9, Room C the next 9, Room D then next 8 and Room E the last 8
17 people and 3 rooms - Room A would have the first 6 people on the list, Room B the next 6, Room C the last 5.
49 people and 4 rooms - A would have first 13, B next 12, C next 12, and D last 12


thanks for looking,
-Ross
 
A little more complexity, but I think it gets you there.

VBA Code:
Sub AssignRoom()
  Dim R As Range
  Dim Cel As Range
  Dim Tot As Long
  Dim Div As Long
  Dim Cnt As Long
  Dim Rooms As Long
  Dim RmCnt As Long
  Dim RmLetters As String
  Dim RmLtr As String
  Dim Rmndr As Long
  Dim DivTemp As Long
 
  RmLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
  Set R = Range(Range("A1"), Range("A1").End(xlDown))
  Tot = R.Rows.Count
  Rooms = InputBox("Please enter the number of rooms", "Room Number")
  'Rooms = Range("E1").Value
  Div = Int(Tot / Rooms)
  Rmndr = Tot Mod Rooms
 
  If Rooms > 26 Then
    MsgBox "There can only be 26 rooms"
    Exit Sub
  End If
 
  RmCnt = 1
  If Rmndr > 0 Then
    DivTemp = Div + 1
    Rmndr = Rmndr - 1
  Else
    DivTemp = Div
  End If
  For Each Cel In R
    Cnt = Cnt + 1
    If Cnt > DivTemp Then
      RmCnt = RmCnt + 1
      Cnt = 1
      If Rmndr > 0 Then
        DivTemp = Div + 1
        Rmndr = Rmndr - 1
      Else
        DivTemp = Div
      End If
    End If
    RmLtr = Mid(RmLetters, RmCnt, 1)
    Cel.Offset(0, 1) = RmLtr
  Next Cel

End Sub
I went with This code.
Thanks Jeff
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Another fun option based to some extent upon the work by jolivanes in Post #10 this is called from a cell and acts similar to a SPILL formula.

It will only work if you have the TEXTSPLIT function on your Excel version.

This is the cell formula.
A call to the fncAssignNameToAGroup function wrapped in the Excel TEXTSPLIT function
=TEXTSPLIT(fncAssignNameToAGroup(B2:B43,A2:A201,TRUE),",","**",TRUE)

It depends on two ranges, one containing the room names and one containing the names of the occupants and a boolean indicating
whether you want the Occupant first (TRUE) or Room Name first (FALSE).

Assign Name to a group.xlsm
ABCDEFGH
1NameRoomsOccupantRoomRoomOccupant
2Name 1Room 1Name 1Room 1Room 1Name 1
3Name 2Room 2Name 2Room 1Room 1Name 2
4Name 3Room 3Name 3Room 1Room 1Name 3
5Name 4Room 4Name 4Room 1Room 1Name 4
6Name 5Room 5Name 5Room 1Room 1Name 5
7Name 6Room 6Name 6Room 2Room 2Name 6
8Name 7Room 7Name 7Room 2Room 2Name 7
9Name 8Room 8Name 8Room 2Room 2Name 8
10Name 9Room 9Name 9Room 2Room 2Name 9
11Name 10Room 10Name 10Room 2Room 2Name 10
12Name 11Room 11Name 11Room 3Room 3Name 11
13Name 12Room 12Name 12Room 3Room 3Name 12
14Name 13Room 13Name 13Room 3Room 3Name 13
15Name 14Room 14Name 14Room 3Room 3Name 14
16Name 15Room 15Name 15Room 3Room 3Name 15
17Name 16Room 16Name 16Room 4Room 4Name 16
18Name 17Room 17Name 17Room 4Room 4Name 17
19Name 18Room 18Name 18Room 4Room 4Name 18
20Name 19Room 19Name 19Room 4Room 4Name 19
21Name 20Room 20Name 20Room 4Room 4Name 20
22Name 21Room 21Name 21Room 5Room 5Name 21
23Name 22Room 22Name 22Room 5Room 5Name 22
24Name 23Room 23Name 23Room 5Room 5Name 23
25Name 24Room 24Name 24Room 5Room 5Name 24
26Name 25Room 25Name 25Room 5Room 5Name 25
27Name 26Room 26Name 26Room 6Room 6Name 26
28Name 27Room 27Name 27Room 6Room 6Name 27
29Name 28Room 28Name 28Room 6Room 6Name 28
30Name 29Room 29Name 29Room 6Room 6Name 29
31Name 30Room 30Name 30Room 6Room 6Name 30
Allocation
Cell Formulas
RangeFormula
D2:E201D2=TEXTSPLIT(fncAssignNameToAGroup(B2:B43,A2:A201,TRUE),",","**",TRUE)
G2:H201G2=TEXTSPLIT(fncAssignNameToAGroup(B2:B43,A2:A201,FALSE),",","**",TRUE)
Dynamic array formulas.


VBA Code:
Public Function fncAssignNameToAGroup(rngRooms As Range, rngNames As Range, blnNameFirst As Boolean) As String
Dim lngRooms As Long
Dim lngPeople As Long
Dim arrAlloc() As String
Dim arrNames() As Variant
Dim arrRooms() As Variant
Dim strString  As String
Dim intName As String
Dim i As Integer
Dim ii As Integer

        lngRooms = rngRooms.Rows.Count
        lngPeople = rngNames.Rows.Count

         If (lngPeople Mod lngRooms) = 0 Then
            arrAlloc = Split(Mid(WorksheetFunction.Rept("," & lngPeople / lngRooms, lngRooms), 2), ",")
        Else
            arrAlloc = Split(Mid(WorksheetFunction.Rept("," & WorksheetFunction.RoundUp(lngPeople / lngRooms, 0), (lngPeople Mod lngRooms)) & _
                WorksheetFunction.Rept("," & (WorksheetFunction.RoundUp(lngPeople / lngRooms, 0) - 1), lngRooms - (lngPeople Mod lngRooms)), 2), ",")
        End If
        
        arrNames = rngNames
        arrRooms = rngRooms
                
        intName = 1
                
        For i = LBound(arrAlloc) To UBound(arrAlloc)
            For ii = 1 To arrAlloc(i)
                If blnNameFirst Then
                    strString = strString & arrNames(intName, 1) & "," & arrRooms(i + 1, 1) & "**"
                Else
                    strString = strString & arrRooms(i + 1, 1) & "," & arrNames(intName, 1) & "**"
                End If
                intName = intName + 1
            Next ii
        Next i
                
        fncAssignNameToAGroup = strString
        
End Function
 
Upvote 0
Another fun option based to some extent upon the work by jolivanes in Post #10 this is called from a cell and acts similar to a SPILL formula.

It will only work if you have the TEXTSPLIT function on your Excel version.

This is the cell formula.
A call to the fncAssignNameToAGroup function wrapped in the Excel TEXTSPLIT function
=TEXTSPLIT(fncAssignNameToAGroup(B2:B43,A2:A201,TRUE),",","**",TRUE)

It depends on two ranges, one containing the room names and one containing the names of the occupants and a boolean indicating
whether you want the Occupant first (TRUE) or Room Name first (FALSE).

Assign Name to a group.xlsm
ABCDEFGH
1NameRoomsOccupantRoomRoomOccupant
2Name 1Room 1Name 1Room 1Room 1Name 1
3Name 2Room 2Name 2Room 1Room 1Name 2
4Name 3Room 3Name 3Room 1Room 1Name 3
5Name 4Room 4Name 4Room 1Room 1Name 4
6Name 5Room 5Name 5Room 1Room 1Name 5
7Name 6Room 6Name 6Room 2Room 2Name 6
8Name 7Room 7Name 7Room 2Room 2Name 7
9Name 8Room 8Name 8Room 2Room 2Name 8
10Name 9Room 9Name 9Room 2Room 2Name 9
11Name 10Room 10Name 10Room 2Room 2Name 10
12Name 11Room 11Name 11Room 3Room 3Name 11
13Name 12Room 12Name 12Room 3Room 3Name 12
14Name 13Room 13Name 13Room 3Room 3Name 13
15Name 14Room 14Name 14Room 3Room 3Name 14
16Name 15Room 15Name 15Room 3Room 3Name 15
17Name 16Room 16Name 16Room 4Room 4Name 16
18Name 17Room 17Name 17Room 4Room 4Name 17
19Name 18Room 18Name 18Room 4Room 4Name 18
20Name 19Room 19Name 19Room 4Room 4Name 19
21Name 20Room 20Name 20Room 4Room 4Name 20
22Name 21Room 21Name 21Room 5Room 5Name 21
23Name 22Room 22Name 22Room 5Room 5Name 22
24Name 23Room 23Name 23Room 5Room 5Name 23
25Name 24Room 24Name 24Room 5Room 5Name 24
26Name 25Room 25Name 25Room 5Room 5Name 25
27Name 26Room 26Name 26Room 6Room 6Name 26
28Name 27Room 27Name 27Room 6Room 6Name 27
29Name 28Room 28Name 28Room 6Room 6Name 28
30Name 29Room 29Name 29Room 6Room 6Name 29
31Name 30Room 30Name 30Room 6Room 6Name 30
Allocation
Cell Formulas
RangeFormula
D2:E201D2=TEXTSPLIT(fncAssignNameToAGroup(B2:B43,A2:A201,TRUE),",","**",TRUE)
G2:H201G2=TEXTSPLIT(fncAssignNameToAGroup(B2:B43,A2:A201,FALSE),",","**",TRUE)
Dynamic array formulas.


VBA Code:
Public Function fncAssignNameToAGroup(rngRooms As Range, rngNames As Range, blnNameFirst As Boolean) As String
Dim lngRooms As Long
Dim lngPeople As Long
Dim arrAlloc() As String
Dim arrNames() As Variant
Dim arrRooms() As Variant
Dim strString  As String
Dim intName As String
Dim i As Integer
Dim ii As Integer

        lngRooms = rngRooms.Rows.Count
        lngPeople = rngNames.Rows.Count

         If (lngPeople Mod lngRooms) = 0 Then
            arrAlloc = Split(Mid(WorksheetFunction.Rept("," & lngPeople / lngRooms, lngRooms), 2), ",")
        Else
            arrAlloc = Split(Mid(WorksheetFunction.Rept("," & WorksheetFunction.RoundUp(lngPeople / lngRooms, 0), (lngPeople Mod lngRooms)) & _
                WorksheetFunction.Rept("," & (WorksheetFunction.RoundUp(lngPeople / lngRooms, 0) - 1), lngRooms - (lngPeople Mod lngRooms)), 2), ",")
        End If
       
        arrNames = rngNames
        arrRooms = rngRooms
               
        intName = 1
               
        For i = LBound(arrAlloc) To UBound(arrAlloc)
            For ii = 1 To arrAlloc(i)
                If blnNameFirst Then
                    strString = strString & arrNames(intName, 1) & "," & arrRooms(i + 1, 1) & "**"
                Else
                    strString = strString & arrRooms(i + 1, 1) & "," & arrNames(intName, 1) & "**"
                End If
                intName = intName + 1
            Next ii
        Next i
               
        fncAssignNameToAGroup = strString
       
End Function

Or if you don't have the TEXTSPLIT function.

VBA Code:
Public Function fncAssignNameToAGroup(rngRooms As Range, rngNames As Range, blnNameFirst As Boolean) As Variant
Dim lngRooms As Long
Dim lngPeople As Long
Dim arrAlloc() As String
Dim arrNames() As Variant
Dim arrRooms() As Variant
Dim arrResult() As Variant
Dim intName As String
Dim i As Integer
Dim ii As Integer

        ReDim arrResult(1 To rngNames.Rows.Count, 1 To 2)
        
        lngRooms = rngRooms.Rows.Count
        lngPeople = rngNames.Rows.Count

         If (lngPeople Mod lngRooms) = 0 Then
            arrAlloc = Split(Mid(WorksheetFunction.Rept("," & lngPeople / lngRooms, lngRooms), 2), ",")
        Else
            arrAlloc = Split(Mid(WorksheetFunction.Rept("," & WorksheetFunction.RoundUp(lngPeople / lngRooms, 0), (lngPeople Mod lngRooms)) & _
                WorksheetFunction.Rept("," & (WorksheetFunction.RoundUp(lngPeople / lngRooms, 0) - 1), lngRooms - (lngPeople Mod lngRooms)), 2), ",")
        End If
        
        arrNames = rngNames
        arrRooms = rngRooms
                
        intName = 1
                        
        For i = LBound(arrAlloc) To UBound(arrAlloc)
            For ii = 1 To arrAlloc(i)
                If blnNameFirst Then
                    arrResult(intName, 1) = arrNames(intName, 1)
                    arrResult(intName, 2) = arrRooms(i + 1, 1)
                Else
                    arrResult(intName, 1) = arrRooms(i + 1, 1)
                    arrResult(intName, 2) = arrNames(intName, 1)
                End If
                intName = intName + 1
            Next ii
        Next i
                
        fncAssignNameToAGroupArray = arrResult
        
End Function
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,040
Members
449,092
Latest member
ikke

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