Option Explicit
Sub TongHopBH()
Dim lRow As Long, RowB As Long, RowC As Long
Dim Rng As Range: Dim StrC As String
Dim SLuong As Double, TTien As Double
Sheets("Data").Select
[a1].Select: lRow = [A65432].End(xlUp).Row
Sheets("TongHop").Range("A3:E" & lRow).Clear
If Selection = "" Then
Set Rng = Range("A1").End(xlDown)
Else
Set Rng = [a1]
End If
Do
Set Rng = Rng.CurrentRegion: StrC = Rng.Cells(1, 1)
RowB = InStr(StrC, "nhân viên") + Len("nhân viên")
StrC = "NV " & Mid(StrC, RowB)
Set Rng = Rng.Cells(3, 1).Resize(Rng.Rows.Count - 2, Rng.Columns.Count)
If Not Rng Is Nothing Then
RowB = Sheets("TongHop").[A65432].End(xlUp).Row + 1
Rng.Copy Destination:=Sheets("TongHop").Range("A" & RowB)
Else
Exit Do
End If
23 ' Them Dong Cong'
RowC = Sheets("TongHop").[A65432].End(xlUp).Row
Sheets("TongHop").Cells(RowC + 1, 1) = [E1] & StrC
FormatRegions Sheets("TongHop").Cells(RowC + 1, 1).Resize(1, 4)
Sheets("TongHop").Cells(RowC + 1, 2).Formula = "=SUM(B" & RowB & ":B" & RowC & ")"
SLuong = SLuong + Sheets("TongHop").Cells(RowC + 1, 2).Value
Sheets("TongHop").Cells(RowC + 1, 4).Formula = "=SUM(d" & RowB & ":d" & RowC & ")"
TTien = TTien + Sheets("TongHop").Cells(RowC + 1, 4).Value
30 ' Den Vung Moi'
Set Rng = Rng.End(xlDown): Set Rng = Rng.End(xlDown)
If Rng.Cells(1, 1).Row > lRow Then Exit Do
Loop
With Sheets("TongHop").Cells(RowC + 2, 1)
.Value = Sheets("Data").[F1]
.Offset(, 1) = SLuong: .Offset(, 3) = TTien
FormatRegions .Resize(1, 4), True
End With
End Sub