Private Sub Worksheet_Change(ByVal Target As Range)
Dim myTable As Variant
With Worksheets("Sheet2")
myTable = .UsedRange.Offset(1).Resize(.UsedRange.Rows.Count - 1).Value
End With
For i = 1 To UBound(myTable, 1) - 1
For j = i + 1 To UBound(myTable, 1)
If myTable(i, 6) >= myTable(j, 6) Then
For k = 1 To UBound(myTable, 2)
temp = myTable(i, k)
myTable(i, k) = myTable(j, k)
myTable(j, k) = temp
Next
End If
Next
Next
For i = 1 To UBound(myTable, 1) - 1
For j = i + 1 To UBound(myTable, 1)
If myTable(i, 4) <= myTable(j, 4) Then
For k = 1 To UBound(myTable, 2)
temp = myTable(i, k)
myTable(i, k) = myTable(j, k)
myTable(j, k) = temp
Select Case k
Case 4
myTable(i, k) = Format$(myTable(i, k), "hh:mm")
myTable(j, k) = Format$(myTable(j, k), "hh:mm")
Case 5
myTable(i, k) = Format$(myTable(j, k), "Medium Time")
myTable(j, k) = Format$(myTable(j, k), "Medium Time")
End Select
Next
End If
Next
Next
Dim classes As Object, days As Object, hours As Object
Set classes = CreateObject("Scripting.Dictionary")
Set days = CreateObject("Scripting.Dictionary")
Set hours = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(myTable, 1)
If Not classes.Exists(Trim(myTable(i, 6))) Then
classes.Add Trim(myTable(i, 6)), 1
End If
Next
r = 2
Dim daysOfWeek As Variant
daysOfWeek = [{"Monday",False;"Tuesday",False;"Wednesday",False;"Thursday",False;"Friday",False;"Saturday",False;"Sunday",False}]
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Worksheets("Schedule").UsedRange.EntireRow.Delete
For i = 0 To classes.Count - 1
For j = 1 To UBound(myTable, 1)
If myTable(j, 6) = classes.Keys()(i) Then
If Not days.Exists(Trim(myTable(j, 1))) Then
days.Add Trim(myTable(j, 1)), 1
End If
If Not hours.Exists(Trim(myTable(j, 4))) Then
hours.Add Trim(myTable(j, 4)), Trim(myTable(j, 2)) & "|" & Trim(myTable(j, 3)) & "|" & Trim(myTable(j, 5)) & "|" & 1
End If
End If
Next
For d = 1 To 7
For k = 0 To days.Count - 1
If days.Keys()(k) = daysOfWeek(d, 1) Then
daysOfWeek(d, 2) = True
End If
Next
Next
With Worksheets("Schedule").Cells(r, 2)
.Value = classes.Keys()(i)
.Offset(1) = "Day"
For d = 1 To 7
If daysOfWeek(d, 2) Then
.Offset(2 + d) = daysOfWeek(d, 1)
End If
Next
For h = 0 To hours.Count - 1
If Split(hours.Items()(h), "|")(1) <> "" Then
.Offset(1, 1 + h) = Split(hours.Items()(h), "|")(1)
.Offset(2, 1 + h) = hours.Keys()(h) & " - " & Split(hours.Items()(h), "|")(2)
Else
.Offset(1, 1 + h) = Split(hours.Items()(h), "|")(0)
.Offset(2, 1 + h) = hours.Keys()(h) & " - " & Split(hours.Items()(h), "|")(2)
End If
temp = Format$(TimeValue(Split(hours.Items()(h), "|")(2)), "hh:mm") & " - " & Format$(TimeValue(Split(hours.Items()(h), "|")(2)) + (20 / 1440), "Medium Time")
hours(hours.Keys()(h)) = h + 1
Next
.Offset(1, 1 + h) = temp
c = 0
For d = 1 To 7
If daysOfWeek(d, 2) = True Then
For h = 0 To hours.Count - 1
For t = 1 To UBound(myTable, 1)
If myTable(t, 3) <> "" Then
If Trim(myTable(t, 1)) = daysOfWeek(d, 1) And Trim(myTable(t, 4)) = hours.Keys()(h) And Trim(myTable(t, 6)) = classes.Keys()(i) Then
.Offset(3 + c, hours.Items()(h)) = Trim(myTable(t, 2))
Exit For
End If
End If
Next
Next
c = c + 1
End If
Next
.Resize(, hours.Count + 2).Merge
.Offset(1).Resize(2).Merge
.Offset(1).HorizontalAlignment = xlCenter
.Offset(1).VerticalAlignment = xlCenter
End With
hours.RemoveAll
r = r + (days.Count + 4)
days.RemoveAll
For d = 1 To 7
daysOfWeek(d, 2) = False
Next
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub