Sub LietKe()
Dim cItem As Collection
Dim kq(1 To 100, 1 To 2) As Variant
Dim lIndex As Long
Dim lTime As Long
Set cItem = New Collection
cItem.Add 1
cItem.Add 3
cItem.Add 6
cItem.Add 8
cItem.Add 12
'cItem.Add 100
kq(1, 1) = "Buoc"
kq(1, 2) = "Thoi gian"
lIndex = 1
Do While cItem.Count <> 0
If cItem.Count = 1 Then
lIndex = lIndex + 1
kq(lIndex, 1) = cItem.Item(1) & "Sang song"
kq(lIndex, 2) = cItem.Item(1)
lTime = cItem(1) + lTime
Exit Do
ElseIf cItem.Count = 2 Then
lIndex = lIndex + 1
kq(lIndex, 1) = "( " & cItem.Item(1) & " , " & cItem(2) & " ) " & " Sang song"
kq(lIndex, 2) = cItem(2)
lTime = cItem(2) + lTime
Exit Do
ElseIf cItem.Count = 3 Then
lIndex = lIndex + 1 '1 2 sang song
kq(lIndex, 1) = "( " & cItem.Item(1) & " , " & cItem(2) & " ) " & " Sang song"
kq(lIndex, 2) = cItem(2)
lTime = cItem(2) + lTime
lIndex = lIndex + 1 '1 quay lai
kq(lIndex, 1) = cItem.Item(1) & "Quay lai"
kq(lIndex, 2) = cItem(1)
lTime = cItem(1) + lTime
lIndex = lIndex + 1 '1 3 sang song
kq(lIndex, 1) = "( " & cItem.Item(1) & " , " & cItem(3) & " ) " & " Sang song"
kq(lIndex, 2) = cItem(3)
lTime = cItem(3) + lTime
Exit Do
Else
lIndex = lIndex + 1 '1 2 sang song
kq(lIndex, 1) = "( " & cItem.Item(1) & " , " & cItem(2) & " ) " & " Sang song"
kq(lIndex, 2) = cItem(2)
lTime = cItem(2) + lTime
lIndex = lIndex + 1 '1 quay lai
kq(lIndex, 1) = cItem.Item(1) & " quay lai"
kq(lIndex, 2) = cItem(1)
lTime = cItem(1) + lTime
lIndex = lIndex + 1 '4 3 sang song
kq(lIndex, 1) = "( " & cItem.Item(cItem.Count - 1) & " , " & cItem(cItem.Count) & " ) " & " Sang song"
kq(lIndex, 2) = cItem(cItem.Count)
lTime = cItem(cItem.Count) + lTime
lIndex = lIndex + 1 '2 quay lai
kq(lIndex, 1) = cItem.Item(2) & " quay lai"
kq(lIndex, 2) = cItem(2)
lTime = cItem(2) + lTime
cItem.Remove cItem.Count
cItem.Remove cItem.Count 'nguoi 1 và nguoi 2 van chua sang song
End If
Loop
Range("o12").Resize(100, 2).Value = kq
Range("o12").Offset(-1).Value = lTime
End Sub