Option Compare Text
Sub XYZ()
Dim sArr(), Res(), KhachHang$
Dim sRow&, sRow_1&, i&, k&
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Sheets("DATA")
sArr = .range("B2:K" & .range("E65000").End(xlUp).row).Value
End With
sRow = UBound(sArr)
ReDim Res(0 To sRow, 1 To 8)
With Sheets("CHI_TIET_131")
.range("A6:H100000").ClearContents
KhachHang = .range("C1").Value
If KhachHang = Empty Then
MsgBox "Khong ton tai ten khach hang!", vbSystemModal, "Thông báo"
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Exit Sub
End If
Res(0, 2) = .range("B5").Value
Res(0, 7) = .range("G5").Value
Res(0, 8) = Res(0, 7)
End With
For i = 1 To sRow
If sArr(i, 4) Like KhachHang Then
k = k + 1
Res(k, 1) = sArr(i, 1)
Res(k, 2) = sArr(i, 3)
Res(k, 3) = sArr(i, 10)
Res(k, 4) = sArr(i, 8)
Res(k, 5) = sArr(i, 7)
Res(k, 6) = sArr(i, 9)
Res(k, 7) = Res(k, 3) - Res(k, 4) - Res(k, 5) - Res(k, 6)
Res(k, 8) = Res(k - 1, 8) + Res(k, 7)
End If
Next i
If k Then
For i = 1 To k
For j = 3 To 7
Res(k + 1, j) = Res(k + 1, j) + Res(i, j)
Next j
Next i
With Sheets("CHI_TIET_131")
.range("A5").Resize(k + 2, 8).Value = Res
.range("C5").Resize(k + 2, 6).NumberFormat = "#,##0_);[Red](#,##0)"
.range("A5").Resize(k + 2, 8).Borders.LineStyle = 1
.range("A5").Resize(k + 2, 8).Borders(xlInsideHorizontal).Weight = xlHairline
End With
MsgBox ("Lay du lieu thanh cong!")
Else
MsgBox "Khong ton tai ten khach hang!", vbSystemModal, "Thông báo"
End If
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub