Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address(0, 0) <> "A1" Or Target.Count > 1 Then Exit Sub
With Target
Application.EnableEvents = False
On Error GoTo CleanUp
Dim msg, Ans, Cancel
Dim rngIn As Range
Dim rngOut As Range
Dim lRow As Long, Col As Long
Dim wkDay As String
If Not Intersect(Range("A1"), .Cells) Is Nothing Then
msg = "If this is a scan IN, Click ""YES"" " & vbCr & vbCr & "If this is a scan OUT, Click ""NO""."
Ans = MsgBox(msg, vbQuestion + vbYesNoCancel)
wkDay = Application.Text(Date, "dddd")
lRow = Cells(Rows.Count, "A").End(xlUp).Row
Col = Application.Match(wkDay, Range("A1:K1"), 0)
Select Case Ans
Case vbYes '*** Scan Time IN *** Yes was clicked
Set rngIn = Sheets("Sheet1").Range("A3:A" & lRow) _
.Find(What:=Sheets("Sheet1").Range("A1").Value, _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If rngIn Is Nothing Then
MsgBox "No match found."
Cells(1, 1).ClearContents
Cells(1, 1).Select
GoTo CleanUp
End If
If rngIn.Offset(, Col - 1) <> "" Then
MsgBox "A time has been scanned in for ID no. " _
& vbCr & vbCr & " " _
& rngIn & vbCr & vbCr & _
" Re-scan and choose ""NO""."
Else
rngIn.Offset(, Col - 1) = Time
End If
Sheets("Sheet1").Cells(1, 1).ClearContents
Sheets("Sheet1").Cells(1, 1).Select
Case vbNo '*** Scan Time OUT *** No was clicked
' wkDay = Weekday(Now())
' lRow = Cells(Rows.Count, "A").End(xlUp).Row
Set rngOut = Sheets("Sheet1").Range("A3:A" & lRow) _
.Find(What:=Sheets("Sheet1").Range("A1").Value, _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If rngOut Is Nothing Then
MsgBox "No match found."
Cells(1, 1).ClearContents
Cells(1, 1).Select
GoTo CleanUp
End If
If rngOut.Offset(, Col - 1) <> "" Then
rngOut.Offset(, Col) = Time
Else
MsgBox "There is no scan IN time, you must scan IN first."
End If
Sheets("Sheet1").Cells(1, 1).ClearContents
Sheets("Sheet1").Cells(1, 1).Select
Case vbCancel '*** Cancel was clicked
Cancel = True
Cells(1, 1).ClearContents
Cells(1, 1).Select
End Select
CleanUp:
Application.EnableEvents = True
End If
End With
End Sub
Sub EnableEvents_Do()
Application.EnableEvents = True
End Sub
Sub Clear_Times_Field()
ActiveSheet.Range(Cells(3, 3), Cells(15, 12)).ClearContents
Cells(1, 1).Select
Application.EnableEvents = True
End Sub