Sub First Dim As Integer Dim As Integer Dim target As String Dim perio

 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
62
63
64
65
66
67
68
69
Sub First()
Dim i As Integer
Dim j As Integer
Dim target As String
Dim period As Integer
For i = 2 To 298 Step 1
Worksheets(2).Activate
target = Cells(i, 4)
period = Cells(i, 3)
Worksheets(3).Activate
For j = 2 To 9 Step 1
If (Cells(j, 1) = target) Then
If (Cells(j, 2) < period) Then
period = Cells(j, 2)
Worksheets(2).Activate
Cells(i, 3) = period
Exit For
End If
End If
Next j
Next i
End Sub
Sub Third()
Dim period As Date
Dim idOfBT As Long 'BT - Business Trip
Dim idOfTheNextBT As Long
i = 2 'counter on first sheet
j = 1 'counter on second sheet
idOfBT = 0
Worksheets(1).Activate
Do While Cells(i, 1) <> ""
If Cells(i, 1) <> idOfBT Then
'new BT
j = j + 1
idOfBT = Cells(i, 1)
Worksheets(2).Activate
period = Cells(j, 2) + Cells(j, 3)
Worksheets(1).Activate
End If
'correction
k = i + 1
Do While (Cells(k, 2) <> Cells(i, 2)) And (Cells(k, 2) <> "") 'searching the next BT's id of accociate
k = k + 1
Loop
If Cells(k, 2) = "" Then
i = i + 1
Else
idOfTheNextBT = Cells(k, 1)
Worksheets(2).Activate
q = j
Do While Cells(q, 1) <> idOfTheNextBT 'searching the row with information about the next BT of accociate
q = q + 1
Loop
periodOfTheNextBT = Cells(q, 2)
Worksheets(1).Activate
If period < periodOfTheNextBT Then
i = i + 1
Else
Rows(k).Delete
i = i + 1
End If
End If
Loop
End Sub