Khi em chạy code msit81_3 có lúc báo lỗi script out of range có lúc không? Quan trọng hơn nữa là khi chạy xong vẫn thấy thiếu phần của tiền EUR ah!
Mong mọi người chỉ giúp!
Đây là đoạn code VBA, mà em mày mò mãi chưa ra ah
Sub msit81_3()
''Dung Dictionary tong hop theo DP_TypeCode
Sheets("msit81_DP").Select
Dim Dic As Object
Dim iRow As Long, I As Long
Dim Arr() As Variant, VungDuLieu As Variant
With Sheets("BaoCaoTheoMSIT81")
.Range("A7:AR45").ClearContents
'''''''''''Tu dong 7 den dong 45
End With
With Sheets("msit81_DP")
Set Dic = CreateObject("Scripting.Dictionary")
VungDuLieu = Range("A1", Range("A1").End(xlToRight).End(xlDown).Address).Value '65536 '1048576
ReDim Arr(1 To UBound(VungDuLieu, 1), 1 To 29)
For iRow = 1 To UBound(VungDuLieu, 1)
j = j + 1
If Not IsEmpty(VungDuLieu(iRow, 5)) And Not Dic.Exists(VungDuLieu(iRow, 5)) Then
I = I + 1
Dic.Add VungDuLieu(iRow, 5), I
Arr(I, 1) = VungDuLieu(iRow, 5) 'Arr(I,1): DPCode
If VungDuLieu(iRow, 10) = "USD" Then
'''''''voi loai tien USD
If VungDuLieu(iRow, 1) = "00" Then
Arr(I, 2) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "03" Then
Arr(I, 5) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "04" Then
Arr(I, 8) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "07" Then
Arr(I, 11) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "09" Then
Arr(I, 14) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "10" Then
Arr(I, 17) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = " " Then
Arr(I, 20) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "05" Then
Arr(I, 23) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "01" Then
Arr(I, 26) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "02" Then
Arr(I, 29) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "06" Then
Arr(I, 32) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "08" Then
Arr(I, 35) = VungDuLieu(iRow, 11)
End If
ElseIf VungDuLieu(iRow, 10) = "VND" Then
If VungDuLieu(iRow, 1) = "00" Then
Arr(I, 3) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "03" Then
Arr(I, 6) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "04" Then
Arr(I, 9) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "07" Then
Arr(I, 12) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "09" Then
Arr(I, 15) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "10" Then
Arr(I, 18) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = " " Then
Arr(I, 21) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "05" Then
Arr(I, 24) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "01" Then
Arr(I, 27) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "02" Then
Arr(I, 30) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "06" Then
Arr(I, 33) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "08" Then
Arr(I, 36) = VungDuLieu(iRow, 11)
End If
ElseIf VungDuLieu(iRow, 10) = "EUR" Then
If VungDuLieu(iRow, 1) = "00" Then
Arr(I, 4) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "03" Then
Arr(I, 7) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "04" Then
Arr(I, 10) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "07" Then
Arr(I, 13) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "09" Then
Arr(I, 16) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "10" Then
Arr(I, 19) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = " " Then
Arr(I, 22) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "05" Then
Arr(I, 25) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "01" Then
Arr(I, 28) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "02" Then
Arr(I, 31) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "06" Then
Arr(I, 34) = VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "08" Then
Arr(I, 37) = VungDuLieu(iRow, 11)
End If
End If
Else
If VungDuLieu(iRow, 10) = "USD" Then
If VungDuLieu(iRow, 1) = "00" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 2) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 2) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "03" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 5) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 5) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "04" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 8) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 8) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "07" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 11) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 11) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "09" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 14) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 14) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "10" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 17) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 17) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = " " Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 20) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 20) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "05" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 23) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 23) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "01" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 26) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 26) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "02" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 29) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 29) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "06" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 32) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 32) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "08" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 35) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 35) + VungDuLieu(iRow, 11)
End If
ElseIf VungDuLieu(iRow, 10) = "VND" Then
If VungDuLieu(iRow, 1) = "00" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 3) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 3) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "03" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 6) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 6) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "04" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 9) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 9) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "07" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 12) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 12) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "09" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 15) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 15) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "10" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 18) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 18) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = " " Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 21) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 21) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "05" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 24) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 24) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "01" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 27) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 27) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "02" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 30) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 30) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "06" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 33) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 33) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "08" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 36) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 36) + VungDuLieu(iRow, 11)
End If
ElseIf VungDuLieu(iRow, 10) = "EUR" Then
If VungDuLieu(iRow, 1) = "00" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 4) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 4) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "03" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 7) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 7) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "04" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 10) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 10) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "07" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 13) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 13) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "09" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 16) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 16) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "10" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 19) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 19) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = " " Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 22) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 22) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "05" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 25) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 25) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "01" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 28) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 28) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "02" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 31) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 31) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "06" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 34) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 34) + VungDuLieu(iRow, 11)
ElseIf VungDuLieu(iRow, 1) = "08" Then
Arr(Dic.Item(VungDuLieu(iRow, 5)), 37) = Arr(Dic.Item(VungDuLieu(iRow, 5)), 37) + VungDuLieu(iRow, 11)
End If
End If
End If
Next iRow
End With
Sheets("BaoCaoTheoMSIT81").Select
With Sheets("BaoCaoTheoMSIT81")
.Range("B9").Resize(I, 37).Value = Arr 'dong nay de xuat gtri mang Arr ra
.Range("C7").Value = "HoiSo-TienGui"
.Range("C7:E7").Merge
.Range("F7").Value = "PGD03-TienGui"
.Range("F7:H7").Merge
.Range("I7").Value = "PGD04-TienGui"
.Range("I7:K7").Merge
.Range("L7").Value = "PGD07-TienGui"
.Range("L7:N7").Merge
.Range("O7").Value = "PGD09-TienGui"
.Range("O7:Q7").Merge
.Range("R7").Value = "PGD10-TienGui"
.Range("R7:T7").Merge
.Range("U7").Value = "IB-TienGui"
.Range("U7:W7").Merge
.Range("X7").Value = "PGD05-TienGui"
.Range("X7:Z7").Merge
.Range("AA7").Value = "PGD01-TienGui"
.Range("AA7:AC7").Merge
.Range("AD7").Value = "PGD02-TienGui"
.Range("AD7:AF7").Merge
.Range("AG7").Value = "PGD06-TienGui"
.Range("AG7:AI7").Merge
.Range("AJ7").Value = "PGD08-TienGui"
.Range("AJ7:AL7").Merge
.Range("C9").Value = "USD"
.Range("D9").Value = "VND"
.Range("E9").Value = "EUR"
.Range("C9:E9").Copy
.Range("F9:AL9").Select
.Paste
.Range("A9").Value = "STT"
.Range("B9").Value = "DPcode"
End With
Application.CutCopyMode = False
Set Dic = Nothing
Sheets("BaoCaoTheoMSIT81").Range("A7:AL" & Cells(Rows.count, 2).End(xlUp).Row).Select
With Selection
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
End With
Sheets("BaoCaoTheoMSIT81").Range("B8:B" & Cells(Rows.count, 2).End(xlUp).Row).Select
With Selection
.ColumnWidth = 4.71
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
'Sap xep thu tu cot DPcode
DongCuoiCuaCot = Cells(Rows.count, 2).End(xlUp).Row
ActiveWorkbook.Worksheets("BaoCaoTheoMSIT81").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("BaoCaoTheoMSIT81").Sort.SortFields.Add key:=Range( _
"B9:B" & DongCuoiCuaCot), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("BaoCaoTheoMSIT81").Sort
.SetRange Range("B9:AL" & DongCuoiCuaCot)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("C10", Range("AL" & DongCuoiCuaCot)).Select
Selection.NumberFormat = "#,##0.00"
Columns("A:AL").AutoFit
Sheets("BaoCaoTheoMSIT81").Range("A8").Select
Application.CutCopyMode = False
End Sub