Sub AdvancedFilter()
Application.ScreenUpdating = False
Dim a(), b(), i, j, k, lr, DK1, DK2, DK3, DK4, jj, lrow
With Sheets("DATA")
a = .Range("A6", .Range("A65000").End(3)).Resize(, 18).Value
lr = UBound(a)
End With
ReDim b(1 To lr, 1 To 22)
With Sheets("DATA")
DK1 = Sheets("Trichloc").Range("B2").Value
DK2 = Sheets("Trichloc").Range("C2").Value
DK3 = Sheets("Trichloc").Range("D2").Value
DK4 = Sheets("Trichloc").Range("E2").Value
On Error Resume Next
For i = 1 To lr
'neu chi? có DK1
If DK2 = "" And DK3 = "" And DK4 = "" Then
' Neu cot 2 bang voi dieu kien 1
If a(i, 2) = DK1 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
'neu chi? có DK2
If DK1 = "" And DK3 = "" And DK4 = "" Then
' Neu cot 5 bang voi dieu kien 2
If a(i, 5) = DK2 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
'neu chi? có DK3
If DK1 = "" And DK2 = "" And DK4 = "" Then
' Neu cot 8 bang voi dieu kien 3
If a(i, 8) = DK3 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = (a(i, 10) - a(i, 14))
b(k, 20) = (a(i, 11) - a(i, 15))
b(k, 21) = (a(i, 12) - a(i, 16))
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' ' neu chi co DK4
If DK1 = "" And DK2 = "" And DK3 = "" Then
' ' Neu cot 17 bang voi dieu kien 4
If a(i, 17) = DK4 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' ' chi co dieu kien 1 và 2
If DK3 = "" And DK4 = "" Then
If a(i, 2) = DK1 And a(i, 5) = DK2 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' chi co dieu kien 1 và 3
If DK2 = "" And DK4 = "" Then
If a(i, 2) = DK1 And a(i, 8) = DK3 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' chi co dieu kien 1 và 4
If DK2 = "" And DK3 = "" Then
If a(i, 2) = DK1 And a(i, 17) = DK4 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' chi co dieu kien 2 và 3
ElseIf DK1 = "" And DK4 = "" Then
If a(i, 8) = DK3 And a(i, 5) = DK2 Then
'
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' chi co dieu kien 2 và 4
ElseIf DK1 = "" And DK3 = "" Then
If a(i, 17) = DK4 And a(i, 5) = DK2 Then
'
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
' chi co dieu kien 3 và 4
ElseIf DK1 = "" And DK2 = "" Then
If a(i, 8) = DK3 And a(i, 17) = DK2 Then
'
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
'
Else 'neu co ca 4 dieu kien
'
If a(i, 2) = DK1 And a(i, 5) = DK2 And a(i, 8) = DK3 And a(i, 7) = DK4 Then
k = k + 1
b(k, 1) = k
For j = 1 To 17
b(k, j) = a(i, j)
Next
b(k, 18) = a(i, 9) - a(i, 13)
b(k, 19) = a(i, 10) - a(i, 14)
b(k, 20) = a(i, 11) - a(i, 15)
b(k, 21) = a(i, 12) - a(i, 16)
b(k, 22) = b(k, 18) + b(k, 19) + b(k, 20) + b(k, 21)
End If
End If
End If
End If
End If
End If
End If
End If
Next i
On Error GoTo 0 'mo lai chuc nang bao loi
With Sheets("Trichloc")
.Range("A8:V500").ClearContents
.Range("A8:V500").Borders.LineStyle = 0
End With
' NEU CODE CHAY THI TRANG TRI BANG 1 TI
If k Then
With Sheets("Trichloc")
.Range("A8").Resize(k, 22) = b
.Range("A8").Resize(k + 2, 22).Borders.LineStyle = 1
End With
Else
Exit Sub
End If
End With
'------------------
' Tinh chinh va dinh dang
With Sheets("Trichloc")
lrow = Sheets("Trichloc").Range("B" & Rows.Count).End(xlUp).Row
.Range("F4").Value = Range("D2").Value
'' .Range("E5").Value = "Tháng " & Right((Range("F8").Value), 7)
.Range("B" & lrow + 1).Value = Range("AA2").Value 'o AA2 co doan text la Tong cong
.Range("B" & lrow + 1).Font.Bold = True
.Range("I" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("I8:I" & lrow))
.Range("J" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("J8:J" & lrow))
.Range("K" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("K8:K" & lrow))
.Range("L" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("L8:L" & lrow))
.Range("M" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("M8:M" & lrow))
.Range("N" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("N8:N" & lrow))
.Range("O" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("O8:O" & lrow))
.Range("P" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("P8:P" & lrow))
.Range("R" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("R8:R" & lrow))
.Range("S" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("S8:S" & lrow))
.Range("T" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("T8:T" & lrow))
.Range("U" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("U8:U" & lrow))
.Range("V" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("V8:V" & lrow))
.Range("C" & lrow + 4) = "Ngày tháng n" & ChrW(259) & "m"
.Range("C" & lrow + 4).WrapText = False
.Range("C" & lrow + 5) = "Ng" & ChrW(432) & ChrW(7901) & "i l" & ChrW(7853) & "p"
.Range("N" & lrow + 4) = "Ngày tháng n" & ChrW(259) & "m"
.Range("N" & lrow + 5) = "Ng" & ChrW(432) & ChrW(7901) & "i duy" & ChrW(7879) & "t"
' .Range("P8:V" & lrow + 1).NumberFormat = "#,##0"
' .Range("P:U" & lrow + 1).Font.Bold = True
'' .Range("C8:C" & lrow).NumberFormat = "mm/yyyy"
'' .Range("F8:F" & lrow).NumberFormat = "mm/yyyy"
' .Range("A3:R" & lrow + 10).Select
' With Selection
' .Font.Name = "Times New Roman"
' .Font.Size = 14
' End With
.Range("a7:V7").Font.Bold = True
' .Range("A8:V" & lrow).Font.Bold = False
.Range("C" & lrow + 10).Value = Range("W3").Value 'tên trung
.Range("N" & lrow + 10).Value = Range("W4").Value 'tên hà
.Range("C" & lrow + 10).Font.Bold = True
.Range("N" & lrow + 10).Font.Bold = True
.Range("A8:V" & lrow + 10).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.EntireRow.AutoFit
End With
'
' .Range("B7:H" & lrow).Select
' With Selection
' .WrapText = True
' .EntireRow.AutoFit
' End With
.Range("B" & lrow).Select
End With
Call settrangin
Application.ScreenUpdating = True
End Sub