Sub LayData()
Dim i As Integer, j As Integer, iCols As Integer
Dim jRows As Integer, iRows As Integer
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
Call TaoTKCT 'Tao Tai khoan chi tiet
S02.Range("a2:X1000").ClearContents
S02.Range("E2:X1000").ClearFormats
iCols = WorksheetFunction.CountA(S02.Range("F1:X1"))
iRows = WorksheetFunction.CountA(S01.Range("SoCT"))
j = 1 'lay so ct
For i = 2 To iRows + 1
If S01.Cells(i - 1, 2) <> S01.Cells(i, 2) Then
j = j + 1
With S02
For k = 1 To 4
.Cells(j, k) = S01.Cells(i, k + 1)
Next k
End With
End If
Next i
'lay sotien
S02.Select
jRows = WorksheetFunction.CountA(S02.Range("A2:A1000"))
For i = 2 To jRows + 1
With S02
.Range("F" & i) = "=SUMPRODUCT((TKDU=Xuat!R1C)*(SoCT=Xuat!RC3)*(SoTien))"
.Range("F" & i).Copy
.Range(Cells(i, 7), Cells(i, iCols + 5)).Select
.Paste
.Range(Cells(i, 5), Cells(i, iCols + 5)).NumberFormat = "#,##0"
Application.CutCopyMode = False
.Range("E" & i) = WorksheetFunction.Sum(Range(Cells(i, 6), Cells(i, iCols + 5)))
End With
S02.Range(Cells(i, 6), Cells(i, iCols + 5)).Value = Range(Cells(i, 6), Cells(i, iCols + 5)).Value
Next i
For j = 5 To iCols + 5 'dong sum
With S02
.Cells(jRows + 2, j) = "=SUM(R2C:R[-1]C)"
.Cells(jRows + 2, j).Font.Bold = True
End With
Next j
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Sub TaoTKCT()
Dim rListSort As Range
S01.Range("X1:X1000").ClearContents
S02.Range("F1", S02.Range("X1").End(xlToRight)).ClearContents
With Range("TKDU")
.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=S01.Range("X1"), Unique:=True
End With
With S01
.Range("X1").Delete
.Names("Extract").Delete
End With
Set rListSort = S01.Range("X1", S01.Range("X1000").End(xlUp))
With rListSort
.Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
rListSort.Copy
With S02
.Range("F1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End With
Application.CutCopyMode = False
Set rListSort = Nothing
S01.Range("X1:X1000").ClearContents
End Sub