Forum Discussion
Excel Macro Assistance Needed for Staff Roster and Training Allocation
- Sep 24, 2023
XLookup is available in Excel in Microsoft 365 and Office 2021. If you have an older version, you can use VLookup:
Sub FillRoster() Dim w1 As Worksheet Dim w2 As Worksheet Dim w3 As Worksheet Dim r1 As Long Dim m1 As Long Dim r2 As Long Dim m2 As Long Dim c1 As Long Dim n1 As Long Dim dt As Date Dim sc As String Dim ar As String Dim v1 Application.ScreenUpdating = False Set w1 = Worksheets("w1") Set w2 = Worksheets("w2") Set w3 = Worksheets("Lists") m1 = w1.Cells(w1.Rows.Count, 1).End(xlUp).Row n1 = w1.Cells(1, w1.Columns.Count).End(xlToLeft).Column v1 = w1.UsedRange.Value For r1 = 3 To m1 v1(r1, 1) = Application.VLookup(v1(r1, 1), w3.Range("A2:B15"), 2, False) For c1 = 5 To n1 v1(r1, c1) = Application.VLookup(v1(r1, c1), w3.Range("D2:E10"), 2, False) Next c1 Next r1 m2 = w2.Cells(w2.Rows.Count, 1).End(xlUp).Row w2.Range(w2.Cells(2, 6), w2.Cells(m2, 8)).ClearContents For r2 = 2 To m2 dt = w2.Cells(r2, 1).Value c1 = w1.Rows(1).Find(What:=dt, LookAt:=xlWhole).Column sc = Application.VLookup(w2.Cells(r2, 4).Value, w3.Range("D2:E10"), 2, False) ar = Trim(w2.Cells(r2, 5).Value) For r1 = 3 To m1 If v1(r1, 1) = ar And v1(r1, c1) = sc Then w2.Cells(r2, 6).Value = v1(r1, 2) w2.Cells(r2, 7).Value = v1(r1, 3) w2.Cells(r2, 8).Value = w1.Cells(r1, c1).Value 'v1(r1, 1) = "" v1(r1, c1) = "" Exit For End If Next r1 Next r2 Application.ScreenUpdating = True End Sub
That looks complicated. It would help to have a copy of the workbook (without sensitive data).
Could you attach it, or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar?
- fajnaAli1580Sep 23, 2023Copper Contributor
Hi Sir,
Kindly find the link below for the Excel sheet to work with.
https://admincreativeapps-my.sharepoint.com/:x:/g/personal/creativeapps_admincreativeapps_onmicrosoft_com/EaqWGX96RSVBpBMUm3wmhq0B-cWSucbRqxdWfn4aVPh5Yw?e=jAeZiN
Thank you.
- HansVogelaarSep 23, 2023MVP
See if the attached version does what you want.
- fajnaAli1580Sep 23, 2023Copper Contributor
Hi Sir,
Thank you so much for your assistance in resolving this issue. It's working amazingly well.
The only change required here is that I need your help to fill in the blank columns as well (see attached picture highlighted in RED).
If a unique staff member is not available for a particular date, I would like the system to select another staff member whose name was previously chosen. In other words, we can duplicate the staff details a second time based on the area and shift time to ensure the supervisor training column is not empty. We should only repeat (duplicate) the staff details if we don't have a staff name and if the column is empty. Please ensure uniqueness as well here.
Everything else you've done has been amazing.
Please assist me in resolving this issue.
Thanks again for your kind help.
- HansVogelaarSep 23, 2023MVP
Thanks - I'll take a look later today.