Sub Test()
Dim bdict As Object, cdict As Object
Dim faddress As String
Dim c As Range, d As Range
Dim minK As Integer: minK = 99
Dim i As Integer: i = 1
Dim rEmpty As Integer
Set bdict = CreateObject("Scripting.Dictionary")
Set cdict = CreateObject("Scripting.Dictionary")
cdict.CompareMode = 1
With cdict
.Add Item:=1, Key:="Block"
.Add Item:=2, Key:="Crankshaft"
.Add Item:=3, Key:="Piston"
.Add Item:=4, Key:="Piston ring"
End With
With ActiveSheet.UsedRange
Set c = .Find("Bike", LookIn:=xlValues, SearchOrder:=xlByColumns)
If Not c Is Nothing Then
faddress = c.Address
Do
bdict.Add Item:=c, Key:=i
i = i + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> faddress
End If
End With
Application.ScreenUpdating = False
For Each Key In bdict.Keys
With bdict.Item(Key)
rEmpty = .End(xlDown).Row
For j = 1 To rEmpty - .Row
If cdict.exists(.Offset(j, 1).Value) Then
If minK > cdict.Item(.Offset(j, 1).Value) Then
minK = cdict.Item(.Offset(j, 1).Value)
Set d = .Offset(j).Resize(1, 2)
End If
End If
Next
minK = 99
d.Copy .Offset(1)
If rEmpty - .Row - 1 >= 1 Then
.Offset(2).Resize(rEmpty - .Row - 1, 2).Delete Shift:=xlUp
End If
End With
Next
Application.ScreenUpdating = True
End Sub