Option Explicit
Sub Loc()
Dim SrcArr, ResArr()
Dim lR As Long, k As Long, lMonthItem As Long, lYearItem As Long
Dim dTargetDate As Date
dTargetDate = Sheet8.Range("K1").Value2 'Sheet nhan du lieu
SrcArr = Sheet2.Range(Sheet2.Range("C4"), Sheet2.Range("C50000").End(xlUp)).Resize(, 20).Value2 'Sheet tong hop bat dau tu cot C tinh la 1
ReDim ResArr(1 To UBound(SrcArr, 1), 1 To 12) 'Pet du lieu tu cot den cot
Application.ScreenUpdating = False ' Khong che vuot qua cot 12
For lR = 1 To UBound(SrcArr, 1) 'nhan du lieu tu
If Len(SrcArr(lR, 1)) Then
lMonthItem = Month(SrcArr(lR, 2))
lYearItem = Year(SrcArr(lR, 2))
If lMonthItem = Month(dTargetDate) Then
If lYearItem = Year(dTargetDate) Then
If SrcArr(lR, 3) <> "0" Then ' "HY"khong cho copi du lieu khi da bao huy, HY cho copi
k = k + 1
If Len(SrcArr(lR, 1)) < 12 Then 'khong che 12 cot copi pet
ResArr(k, 1) = String(7 - Len(SrcArr(lR, 1)), "0") & SrcArr(lR, 1) 'khong che them bay so 0
Else
ResArr(k, 1) = SrcArr(lR, 1)
End If
ResArr(k, 2) = SrcArr(lR, 2) 'Ngay
ResArr(k, 3) = SourceToDest(SrcArr(lR, 7), 3, 1) 'Nguoi mua hang
ResArr(k, 4) = CStr(SrcArr(lR, 5)) 'Ma so thue
ResArr(k, 5) = Round(SrcArr(lR, 16), 0) 'Tien hang
ResArr(k, 6) = Round(SrcArr(lR, 17), 0) 'Tien thue
ResArr(k, 11) = SrcArr(lR, 3) 'bao huy
ResArr(k, 12) = SourceToDest(SrcArr(lR, 15), 3, 1) ' Ten hang
End If
End If
End If
End If
Next lR
If k Then
With Sheet8
.Range("C3:N10000").ClearContents 'Pet vao tu cot den cot
.Range("C3").Resize(k, 12).Value = ResArr ' nhan du lieu tu C3 den cot thu 12
End With
End If
Application.ScreenUpdating = True
End Sub
'------------------------------------------
'Ke khung
Range("C3", "I" & n + 2).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("C3", "I" & n).Select
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Range("C" & n + 1, "I" & n + 1).Select
Selection.Borders(xlInsideVertical).LineStyle = xlNone
End With
End If
Application.ScreenUpdating = True
End Sub
Sub Loc()
Dim SrcArr, ResArr()
Dim lR As Long, k As Long, lMonthItem As Long, lYearItem As Long
Dim dTargetDate As Date
dTargetDate = Sheet8.Range("K1").Value2 'Sheet nhan du lieu
SrcArr = Sheet2.Range(Sheet2.Range("C4"), Sheet2.Range("C50000").End(xlUp)).Resize(, 20).Value2 'Sheet tong hop bat dau tu cot C tinh la 1
ReDim ResArr(1 To UBound(SrcArr, 1), 1 To 12) 'Pet du lieu tu cot den cot
Application.ScreenUpdating = False ' Khong che vuot qua cot 12
For lR = 1 To UBound(SrcArr, 1) 'nhan du lieu tu
If Len(SrcArr(lR, 1)) Then
lMonthItem = Month(SrcArr(lR, 2))
lYearItem = Year(SrcArr(lR, 2))
If lMonthItem = Month(dTargetDate) Then
If lYearItem = Year(dTargetDate) Then
If SrcArr(lR, 3) <> "0" Then ' "HY"khong cho copi du lieu khi da bao huy, HY cho copi
k = k + 1
If Len(SrcArr(lR, 1)) < 12 Then 'khong che 12 cot copi pet
ResArr(k, 1) = String(7 - Len(SrcArr(lR, 1)), "0") & SrcArr(lR, 1) 'khong che them bay so 0
Else
ResArr(k, 1) = SrcArr(lR, 1)
End If
ResArr(k, 2) = SrcArr(lR, 2) 'Ngay
ResArr(k, 3) = SourceToDest(SrcArr(lR, 7), 3, 1) 'Nguoi mua hang
ResArr(k, 4) = CStr(SrcArr(lR, 5)) 'Ma so thue
ResArr(k, 5) = Round(SrcArr(lR, 16), 0) 'Tien hang
ResArr(k, 6) = Round(SrcArr(lR, 17), 0) 'Tien thue
ResArr(k, 11) = SrcArr(lR, 3) 'bao huy
ResArr(k, 12) = SourceToDest(SrcArr(lR, 15), 3, 1) ' Ten hang
End If
End If
End If
End If
Next lR
If k Then
With Sheet8
.Range("C3:N10000").ClearContents 'Pet vao tu cot den cot
.Range("C3").Resize(k, 12).Value = ResArr ' nhan du lieu tu C3 den cot thu 12
End With
End If
Application.ScreenUpdating = True
End Sub
'------------------------------------------
'Ke khung
Range("C3", "I" & n + 2).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("C3", "I" & n).Select
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Range("C" & n + 1, "I" & n + 1).Select
Selection.Borders(xlInsideVertical).LineStyle = xlNone
End With
End If
Application.ScreenUpdating = True
End Sub

