Option Explicit
Sub Loc()
Dim Lr&, i&, Lr1&, Lr2&, Lr3&
Dim Key As String
Dim Sh As Worksheet, Ws As Worksheet
Application.ScreenUpdating = False
Set Sh = Sheets("Data")
'Sh.Select
'Selection.AutoFilter
Lr = Sh.Cells(Rows.Count, "A").End(xlUp).Row + 1
Sh.Range("A3:A" & Lr - 1).Copy Sheets("KQ").Range("R1")
Sh.Range("C" & Lr) = "Total"
Sh.Range("D" & Lr & " :F" & Lr).FormulaR1C1 = "=SUBTOTAL(9,R[-" & Lr - 3 & "]C:R[-1]C)"
Sh.Range("G" & Lr).FormulaR1C1 = "=SUM(RC[-3]:RC[-1])"
Set Ws = Sheets("KQ")
Lr1 = Ws.Cells(Rows.Count, "R").End(xlUp).Row
Ws.Range("R1:R" & Lr1).RemoveDuplicates Columns:=1, Header:=xlNo
Lr2 = Ws.Cells(Rows.Count, "R").End(xlUp).Row
Ws.Range("I3:O10000").ClearContents
For i = 1 To Lr2
Key = Ws.Range("R" & i): Lr3 = Ws.Cells(Rows.Count, "O").End(xlUp).Row
Sh.Range("A2").AutoFilter
Sh.Range("A2:F" & Lr - 1).AutoFilter Field:=1, Criteria1:=Key
Sh.Range("A3:G" & Lr).Copy
Ws.Range("I" & Lr3 + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
Next i
Sheets("Data").Select
Selection.AutoFilter
Sh.Range("C" & Lr & " :G" & Lr).ClearContents
Application.ScreenUpdating = True
MsgBox "Done"
End Sub