Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRANGE As Range, Cnt As Long, DayCol As Long, EmpRow As Long, JobCol As Long
Dim TimeOff As Range, CurDay As String, Job As String
'Make sure the changed cell requires analysis
If Not Intersect(Target, Range("J7:J165, Z7:Z165")) Is Nothing And Target.Cells(1, 1).Value <> "" Then
On Error Resume Next
'store the name of today so we can look it up later
CurDay = Format([H1], "DDDD")
'count how many times this name appears in the current column
Cnt = Application.WorksheetFunction.CountIf(Range(Cells(7, Target.Column), Cells(165, Target.Column)), Target)
Application.EnableEvents = False
If Cnt > 1 Then 'if the name appears more than once, remove it
MsgBox "This employee has already been scheduled during this " & CurDay & " shift.", vbExclamation + vbOKOnly, "Duplicate Shift"
Target = ""
Target.Activate
GoTo Finished
End If
'check timeoff requests
'Find the day being scheduled in the TimeOff sheet, which column needs to be checked
DayCol = Sheets("TimeOff").Rows(3).Find(CurDay, LookIn:=xlValues, LookAt:=xlWhole).Column
'Try to find the current name entered in the correct column of TimeOff sheet
Set TimeOff = Sheets("TimeOff").Columns(DayCol).Find(Target, LookIn:=xlValues, LookAt:=xlWhole)
'if the name WAS found, do not allow to be scheduled
If Not TimeOff Is Nothing Then
MsgBox "This employee has requested this time off for " & CurDay & ".", vbExclamation + vbOKOnly, "Time Off"
Target = ""
Target.Activate
GoTo Finished
End If
'Check the employee info sheet availability column for this day
With Sheets("EmpInfo")
'Find the correct column for this day
DayCol = .Rows(1).Find(CurDay, LookIn:=xlValues, LookAt:=xlWhole).Column
'find the correct row for this employee
EmpRow = .Range("C:C").Find(Target, LookIn:=xlValues, LookAt:=xlWhole).Row
'check if the begin time is too early, or the end time too late
If Round(.Cells(EmpRow, DayCol), 6) > Round(Target.Offset(, -8).Value, 6) Or _
Round(.Cells(EmpRow, DayCol + 1), 6) < Round(Target.Offset(, -4).Value, 6) Then
'if it's outside available times, disallow the shift
MsgBox Target & " availability for " & CurDay & " is:" & vbLf & vbLf & _
" Start: " & Format(.Cells(EmpRow, DayCol), "HH:MM AM/PM") & vbLf & _
" End: " & Format(.Cells(EmpRow, DayCol + 1), "HH:MM AM/PM"), vbExclamation + vbOKOnly, "Not Available"
Target = ""
Target.Activate
GoTo Finished
End If
'check job function against the employees certified jobs, must list as TRUE to allow
'find the column for the current job being scheduled
JobCol = .Rows(1).Find([A6], LookIn:=xlValues, LookAt:=xlWhole).Column
'if the employees row does not say TRUE in that column, warning message
If Not .Cells(EmpRow, JobCol) Then
'message will allow them to override the certification and schedule it anyway
If MsgBox("This employee is not certified for this position." & vbLf & _
"Schedule them anyway?", vbExclamation + vbYesNo, "Time Off") = vbNo Then
Target = ""
Target.Activate
GoTo Finished
End If
End If
End With
End If
Finished:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("J7:J165, Z7:Z165")) Is Nothing And Target.Cells(1, 1).Value <> "" Then
If Target.Cells(1, 1).Offset(165) = "yes" Then
Application.EnableEvents = False
Application.Undo
Target.Select
Application.EnableEvents = True
End If
End If
End Sub
Bookmarks