Private Sub Worksheet_Change(ByVal Target As Range)
'Validate if the date input cell was changed
If Intersect(Target, Range("A1")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
'Declare sheet references
Dim resultSheet As Worksheet, thisSheet As Worksheet
'Check for a previously created results sheet and delete it
On Error Resume Next
Set resultSheet = Worksheets("Results")
If Not resultSheet Is Nothing Then
Application.DisplayAlerts = False
resultSheet.Delete
End If
On Error GoTo 0
'set sheet references
Set thisSheet = ActiveSheet
Set resultSheet = Worksheets.Add(After:=thisSheet)
resultSheet.Name = "Results"
'copy table header
thisSheet.Range("C1:E1").Copy resultSheet.Range("A1")
'Declare row number references
Dim sourceRow As Long, endSourceRow As Long, resultRow As Long
'Determine end of source table
resultRow = 2
endSourceRow = thisSheet.Cells(thisSheet.Rows.Count, 3).End(xlUp).Row
'Declare date references
Dim admitted As Date, endDate As Date
Dim hours As Double
'loop through list of patients and copy to new table on result sheet
For sourceRow = 2 To endSourceRow
admitted = thisSheet.Cells(sourceRow, 4)
hours = thisSheet.Cells(sourceRow, 5)
endDate = DateAdd("h", hours, admitted)
If Target >= admitted And Target <= endDate Then
thisSheet.Range(thisSheet.Cells(sourceRow, 3), thisSheet.Cells(sourceRow, 5)).Copy resultSheet.Cells(resultRow, 1)
resultRow = resultRow + 1
End If
Next sourceRow
'get rid of the walking ants animation
Application.CutCopyMode = False
End Sub