Sub Tri()
Dim RngTri As Range
Columns(3).Insert
Set RngTri = [A2].Resize([A1000000].End(xlUp).Row - 1, 3)
RngTri.Sort Key1:=RngTri.Columns(2), Order1:=xlAscending, Key2:=RngTri.Columns(1), Order2:=xlAscending
With RngTri.Columns(3): .FormulaR1C1 = "=IF(RC2<>R[-1]C2,RC1,R[-1]C)": .Value = .Value: End With
RngTri.Sort Key1:=RngTri.Columns(3), Order1:=xlAscending, Key2:=RngTri.Columns(2), Order2:=xlAscending
Columns(3).Delete
End Sub