Dim Count As Long
Dim Found As Boolean
Dim TempArray() As Variant
Private Sub Workbook_Open()
ArrayCount = 0
Limit = 1
Found = False
ReDim TimeEvents(3, 0)
While Found = False
If Sheets("Stations").Cells(Limit + 1, 1) = "" Then Found = True Else Limit = Limit + 1
Wend
Found = False
While Found = False
GracePeriod = InputBox("Please enter your preferred grace period in minutes", "Select Grace Period", 10)
If IsNumeric(GracePeriod) Then
GracePeriod = Int(GracePeriod)
If GracePeriod < 31 Then Found = True
End If
If Found = False Then MsgBox ("The grace period you have chosen is invalid. The maximum allowable grace period is thirty minutes.")
Wend
For Count = 1 To Limit - 1
ReDim Preserve TimeEvents(3, (2 * Count) - 1)
With Sheets("Stations")
TimeEvents(0, (2 * Count) - 2) = .Cells(Count + 1, 1)
TimeEvents(1, (2 * Count) - 2) = .Cells(Count + 1, 4)
TimeEvents(2, (2 * Count) - 2) = .Cells(Count + 1, 2) + TimeSerial(0, GracePeriod, 0)
TimeEvents(3, (2 * Count) - 2) = "Open"
TimeEvents(0, (2 * Count) - 1) = .Cells(Count + 1, 1)
TimeEvents(1, (2 * Count) - 1) = .Cells(Count + 1, 4)
TimeEvents(2, (2 * Count) - 1) = .Cells(Count + 1, 3) + TimeSerial(0, GracePeriod, 0)
TimeEvents(3, (2 * Count) - 1) = "Close"
End With
Next
' Bubble Sort Time Events
ReDim TempArray(3, 1)
Limit = UBound(TimeEvents, 2)
For i = 0 To Limit
For j = i + 1 To Limit
If TimeEvents(2, i) > TimeEvents(2, j) Then
For k = 0 To 3
TempArray(k, 0) = TimeEvents(k, i)
TimeEvents(k, i) = TimeEvents(k, j)
TimeEvents(k, j) = TempArray(k, 0)
Next
End If
Next j
Next
Found = False
Limit = 2
While Found = False
If Sheets("Events").Cells(Limit + 1, 1) = "" Then Found = True Else Limit = Limit + 1
Wend
If TimeEvents(2, ArrayCount) <= TimeValue(Now) Then Application.OnTime Now + TimeValue("00:00:01"), "Reminder" Else Application.OnTime TimeEvents(2, ArrayCount), "Reminder"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Application.OnTime dtime, "RemindMe", , False
Me.Save
End Sub