forked from Vitosh/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActivitySelectionProblem.vb
61 lines (47 loc) · 1.77 KB
/
ActivitySelectionProblem.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Option Explicit
Public Sub TestMe()
Dim objA As clsActivity
Dim colObjs As New Collection
Dim rngCell As Range
Dim strResult As String
Dim i As Long
Dim lngNextStart As Long: lngNextStart = 0
For Each rngCell In Range(Cells(1, 1), Cells(1, 11))
Set objA = Nothing
Set objA = New clsActivity
objA.StartTime = rngCell
objA.EndTime = rngCell.Offset(1, 0)
objA.Name = rngCell.Offset(2, 0)
colObjs.Add objA
Next rngCell
Set colObjs = SortedCollection(colObjs)
For i = 1 To colObjs.Count
If colObjs.Item(i).StartTime > lngNextStart Then
strResult = strResult & colObjs.Item(i).Name & vbTab & _
colObjs.Item(i).StartTime & vbTab & _
colObjs.Item(i).EndTime & vbCrLf
lngNextStart = colObjs.Item(i).EndTime
End If
Next i
Debug.Print strResult
End Sub
Public Function SortedCollection(myColl As Collection, Optional blnSortABC As Boolean = True) As Collection
Dim i As Long
Dim j As Long
For i = myColl.Count To 2 Step -1
For j = 1 To i - 1
If blnSortABC Then
If myColl(j).EndTime > myColl(j + 1).EndTime Then
myColl.Add myColl(j), after:=j + 1
myColl.Remove j
End If
Else
If myColl(j).EndTime < myColl(j + 1).EndTime Then
myColl.Add myColl(j), after:=j + 1
myColl.Remove j
End If
End If
Next j
Next i
Set SortedCollection = myColl
End Function