help me write a vba code for a work from office roster. The roster is to ensure that the 12 employees - Achal, Dhaya, Darshan, Dinu, Gururaj, Sudeshna, Shivangi, Sowmya, Sajas, Raj, Vineet and Sridhar are meeting their monthly mandate of 50% working from office. 1. The offices are closed on Weekends and public holidays. There are 2 office locations - M3 and ETV. 2. There are 2 seats available in ETV on every work day. 3. There are 4 seats available in M3 on every work day. 4. Achal, Dinu, Sowmya and Sridhar prefer to work from M3. 5. For the rest 8 employees, ETV and M3 are shared locations. 6. Dinu can work consecutively for 2 work weeks to meet the mandate. 7. Sajas and Gururaj can work 3 work days for 3 weeks to meet the mandate. 8. No employee should work for more than 50% of the work days per month. 9. No employee should work for less than 50% of the work days per month. 10. The excel template should be in calendar format. 11. The excel template should summarize the total work days for each employee and their progress in percentage to meet the 50% requirement.
Assistant
Creating a VBA code to generate a work-from-office roster considering all constraints can become quite complex, but I'll provide a basic structure that you can adjust as needed. The code will focus on generating a roster on a worksheet named "Roster" and providing a summary on a worksheet named "Summary".
1. Prepare the Excel Template
Create a worksheet named "Roster" in your Excel workbook.
Set up the calendar for a month:
Row 1: Dates of the month.
Column A: Employee Names.
Create another worksheet named "Summary":
Column A: Employee Names.
Column B: Total Days Worked.
Column C: Progress Percentage.
2. VBA Code
Here's the basic VBA code structure to achieve your requirements:
vba
Sub GenerateRoster()
Dim wsRoster As Worksheet
Dim wsSummary As Worksheet
Dim empNames As Variant
Dim empPref As Variant
Dim empCounts As Collection
Dim i As Long, j As Long
Dim totalWorkDays As Integer
Dim dateCol As Integer
Dim seatM3 As Integer, seatETV As Integer
Set wsRoster = ThisWorkbook.Sheets("Roster")
Set wsSummary = ThisWorkbook.Sheets("Summary")
' Define employee names and their preferences
empNames = Array("Achal", "Dhaya", "Darshan", "Dinu", "Gururaj", "Sudeshna", _
"Shivangi", "Sowmya", "Sajas", "Raj", "Vineet", "Sridhar")
empPref = Array("M3", "", "", "M3", "", "", "", "M3", "", "", "", "M3")
Set empCounts = New Collection
For i = LBound(empNames) To UBound(empNames)
empCounts.Add 0, empNames(i) ' Initialize employee work day counts
Next i
' Define the range of work days in the month (excluding weekends)
dateCol = 2 ' Assuming dates start from column B
totalWorkDays = 20 ' Change this according to the number of working days
' Go through each day and assign employees to locations
For j = dateCol To dateCol + totalWorkDays - 1
seatM3 = 0
seatETV = 0
For i = LBound(empNames) To UBound(empNames)
' Check if the employee can work on this day
If CanWork(empNames(i), empCounts(empNames(i)), j - dateCol + 1) Then
' Assign employee to preferred location if available, otherwise to shared
If empPref(i) = "M3" And seatM3 < 4 Then
wsRoster.Cells(i + 2, j).Value = "M3"
seatM3 = seatM3 + 1
ElseIf empPref(i) <> "M3" And seatETV < 2 Then
wsRoster.Cells(i + 2, j).Value = "ETV"
seatETV = seatETV + 1
ElseIf seatM3 < 4 Then
wsRoster.Cells(i + 2, j).Value = "M3"
seatM3 = seatM3 + 1
ElseIf seatETV < 2 Then
wsRoster.Cells(i + 2, j).Value = "ETV"
seatETV = seatETV + 1
End If
' Increment work day count for the employee
empCounts(empNames(i)) = empCounts(empNames(i)) + 1
End If
Next i
Next j
' Update the summary worksheet with results
For i = LBound(empNames) To UBound(empNames)
wsSummary.Cells(i + 2, 1).Value = empNames(i)
wsSummary.Cells(i + 2, 2).Value = empCounts(empNames(i))
wsSummary.Cells(i + 2, 3).Value = Format((empCounts(empNames(i)) / totalWorkDays) * 100, "0.00") & "%"
Next i
MsgBox "Roster generated successfully!"
End Sub
Function CanWork(empName As String, currentDays As Integer, currentDay As Integer) As Boolean
' Implement logic to check if an employee can work on a specific day
' Considering the special rules for Dinu, Sajas, and Gururaj
' Example code to be completed with actual logic:
CanWork = False ' Set the default to false
' Example ensuring no overbooking:
If currentDays < 10 Then ' 50% of 20 working days
Select Case empName
Case "Dinu"
' Logic for Dinu's special rule
' and similarly implement for other specific cases...
Case "Sajas", "Gururaj"
' Logic for their special rule
Case Else
CanWork = True
End Select
End If
End Function
Important Notes
Custom Logic: You need to fill in the specific logic for CanWork based on your special rules for employees like Dinu, Sajas, and Gururaj.
Calendar Adjustment: You'll also need to adjust the number of totalWorkDays and account for any public holidays manually.
Testing Required: Thoroughly test the script as this is a complex scheduling task, and you may need to tweak the logic for your specific use case.
Calendar Setup: Ensure your dates in the "Roster" worksheet and employee names are correctly aligned as per the indices used in the code.